There are two different ways to implement a thread

Source: Internet
Author: User
There are two ways to implement multithreading in Java. The first is the direct inheritance of the thread class, and the second is to implement Runnable interface. So what is the difference between the two ways of implementing multithreading?
The first way: Create a thread using the Runnable interface

Second way: Directly inherit the thread class to create an object

Creating a thread using the Runnable interface

1. Can separate the CPU, code and data to form a clear model

2. The class where the thread body run () method resides can inherit some useful properties and methods from other classes

3. To maintain the consistent design style of the program

Directly inherit the thread class to create an object

1.Thread subclasses can no longer inherit from other classes (Java language single inheritance).

2. Write simple, the current object of the run () method is the thread object, can be directly manipulated.

In practical applications, almost all of them take the first approach
We use the code to simulate the railway ticketing system, to achieve the sale of a day by four ticket sales of a train 100 tickets, a ticket point with a thread.

We begin by writing this procedure:

Java code class ThreadTest extends thread{private int ticket = 100; public void Run () {while (true) {if (Ticket > 0) {System.out.println (Thread.CurrentThread (). Getna         Me () + "is saling ticket" + ticket--);         }else{break; }       }     }   }
Main test class:
Java code public class threaddome1{public static void Main (string[] args) {threadtest t = new threadtest ();       T.start ();       T.start ();       T.start ();     T.start (); }   }
In the above code, we use the ThreadTest class to simulate the ticket sales process, each cycle of the run method will reduce the total number of votes by 1, simulated sell a ticket, and the car ticket number printed out, directly remaining votes to zero so far. In the main method of the ThreadDemo1 class, we created a thread object and started it repeatedly four times, hoping to generate four threads in this way. From the running results we found that only one thread is running, the result tells us: A thread object can only start a thread, no matter how many times you call the start () method, the result is only one thread.

We then modified the THREADDEMO1 to create four thread objects in the Main method:
Java code public class threaddemo1{public static void Main (string[] args) {new ThreadTest (). Start ();       New ThreadTest (). Start ();       New ThreadTest (). Start ();     New ThreadTest (). Start (); }   }
Java code class ThreadTest extends thread{private int ticket = 100; public void Run () {while (true) {if (Ticket > 0) {System.out.println (Thread.CurrentThread (). Getna         Me () + "is saling ticket" + ticket--);         }else{break; }       }     }   }

Is this the end of the meeting?

The results show that each ticket is printed four times, that is, four threads each sell their own 100 tickets, instead of selling a common 100 tickets. How did this happen? What we need is that multiple threads deal with the same resource, a resource can only correspond to one object, in the above program, we create four threadtest objects, it is equal to create four resources, each resource has 100 tickets, each thread is working on its own resources.

Through these experiments and analysis, it can be concluded that to achieve this railway ticketing process, we can only create a resource object, but to create multiple threads to process the same resource object, and each thread is running the same program code. Review the process of writing multithreading using interfaces.
Java code public class threaddemo1{public static void Main (string[] args) {threadtest t = new threadtest ();       New Thread (t). Start ();       New Thread (t). Start ();       New Thread (t). Start ();     New Thread (t). Start (); }

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.