Using inherited classes and runnable interfaces in Java to realize multithreading differences

Source: Internet
Author: User

Multithreading with inherited thread classes cannot be shared with resources

The following procedure

Class MyThread extends thread{
Public MyThread (String name) {
Super ();
This.name=name;
}
public void Run () {
while (Tickets==0?false:true)
{
tickets--;
System.out.println (name+ "Just selt one ticket,only" +tickets+ "left");
try {
Thread.Sleep (100);
} catch (Exception e) {
E.printstacktrace ();
}
}
}
private int tickets=10;
private String name;
}
public class selltickets{
public static void Main (string[] args) {
MyThread amythread1=new MyThread ("MyThread 1");
MyThread amythread2=new MyThread ("MyThread num 2");
Amythread1.start ();
Amythread2.start ();
}
}

The printing results are as follows:

MyThread 1 Just selt one ticket,only 9 left
MyThread Num 2 just selt one ticket,only 9 left
MyThread 1 just Selt One ticket,only 8 left
MyThread Num 2 just selt one ticket,only 8 left
MyThread Num 2 just selt one ticket,only 7 le FT
MyThread 1 just selt one ticket,only 7 left
MyThread Num 2 just selt one ticket,only 6 left
MyThread 1 just SE Lt One ticket,only 6 left
MyThread Num 2 just selt one ticket,only 5 left
MyThread 1 just selt one ticket,only 5 lef T
MyThread 1 just selt one ticket,only 4 left
MyThread Num 2 just selt one ticket,only 4 left
MyThread 1 Just Sel T one ticket,only 3 left
MyThread num 2 just selt one ticket,only 3 left
MyThread 1 just selt one ticket,only 2 left
MyThread Num 2 just selt one ticket,only 2 left
MyThread 1 just selt one ticket,only 1 left
MyThread Num 2 Just Selt one ticket,only 1 left
MyThread 1 just selt one ticket,only 0 left
MyThread Num 2 just selt one ticket,only 0 L EFT

It can be seen that there are actually only 10 tickets, but two threads sold 10 tickets, sold 20.

Of course, you can also define tickets as static, that is, private static int tickets=10; You can do it for 10 tickets.

The following are implemented with the Runnable interface:

Class MyThread implements runnable{
Public MyThread (String name) {
Super ();
This.name=name;
}
public void Run () {
while (Tickets==0?false:true) {
tickets--;
System.out.println (Thread.CurrentThread (). GetName () + "just selt one ticket,only" +tickets+ "left");
try {
Thread.Sleep (100);
} catch (Exception e) {
E.printstacktrace ();
}
}
}
private String name;
private int tickets=10;
}
public class sellticketsrunnable{
public static void Main (string[] args) {
MyThread amythread=new MyThread ("My Thread");
New Thread (Amythread). Start ();
New Thread (Amythread). Start ();
}
}

The print interface is as follows:

Thread-0 just selt one ticket,only 9 left
Thread-1 just selt one ticket,only 8 left
Thread-0 just selt one ticket,only 7 left
Thread-1 just selt one ticket,only 6 left
Thread-0 just selt one ticket,only 5 left
Thread-1 just Selt one ticket,only 4 left
Thread-0 just Selt one ticket,only 3 left
Thread-1 just selt one ticket,only 2 left
Thread-0 just selt one ticket,only 1 left
Thread-1 just selt one ticket,only 0 left

It can be seen that although there are two threads, the construction parameters of two threads are the same object, accessing the resources of the same object. Sold 10 tickets and reached the resource sharing

Using inherited classes and runnable interfaces in Java to realize multithreading differences

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.