There are two ways to create thread threads in Java:
1. By inheriting the thread class, overriding the thread's run () method, placing the logic in which the threads run
2. Instantiate the thread class by implementing the Runnable interface
In the practical application, we often use multi-threading, such as the station ticketing system, the station's various ticket outlets equal to each thread. When we do this system may think of two ways to achieve, inherit the thread class or implement the Runnable interface, now look at the two ways to achieve the results.
Java code
- Package com.threadtest;
- Class MyThread extends thread{
- private int ticket = 10;
- private String name;
- Public MyThread (String name) {
- THIS.name =name;
- }
- public void Run () {
- for (int i =0;i<500;i++) {
- if (this.ticket>0) {
- SYSTEM.OUT.PRINTLN (this.name+ "sell ticket---->" + (this.ticket--));
- }
- }
- }
- }
- public class Threaddemo {
- public static void Main (string[] args) {
- MyThread mt1= New MyThread ("one Window");
- MyThread mt2= New MyThread ("Window No. second");
- MyThread mt3= New MyThread ("Window No. third");
- Mt1.start ();
- Mt2.start ();
- Mt3.start ();
- }
- }
The results of the operation are as follows:
Java code
- A window sells tickets---->10
- A window sells tickets---->9
- Window second sell ticket---->10
- A window sells tickets---->8
- A window sells tickets---->7
- A window sells tickets---->6
- Window third sell ticket---->10
- A window sells tickets---->5
- A window sells tickets---->4
- A window sells tickets---->3
- A window sells tickets---->2
- A window sells tickets---->1
- Window second sell ticket---->9
- Window second sell ticket---->8
- Window third sell ticket---->9
- Window third sell ticket---->8
- Window third sell ticket---->7
- Window third sell ticket---->6
- Window third sell ticket---->5
- Window third sell ticket---->4
- Window third sell ticket---->3
- Window third sell ticket---->2
- Window third sell ticket---->1
- Window second sell ticket---->7
- Window second sell ticket---->6
- Window second sell ticket---->5
- Window second sell ticket---->4
- Window second sell ticket---->3
- Window second sell ticket---->2
- Window second sell ticket---->1
The code for implementing the Runnable interface is as follows:
Java code
- Package com.threadtest;
- Class MyThread1 implements runnable{
- private int ticket = 10;
- private String name;
- public void Run () {
- for (int i =0;i<500;i++) {
- if (this.ticket>0) {
- System.out.println (Thread.CurrentThread (). GetName () + "sell ticket---->" + (this.ticket--));
- }
- }
- }
- }
- public class Runnabledemo {
- public static void Main (string[] args) {
- TODO auto-generated Method Stub
- Design of three threads
- MyThread1 MT = new MyThread1 ();
- thread T1 = new Thread (MT, "one Window");
- Thread t2 = new Thread (MT, "window number second");
- thread t3 = new Thread (MT, "window number third");
- MyThread1 mt2 = new MyThread1 ();
- MyThread1 mt3 = new MyThread1 ();
- T1.start ();
- T2.start ();
- T3.start ();
- }
- }
The results of the operation are as follows:
Java code
- A window sells tickets---->10
- Window third sell ticket---->9
- Window third sell ticket---->7
- Window third sell ticket---->5
- Window third sell ticket---->4
- Window third sell ticket---->3
- Window third sell ticket---->2
- Window third sell ticket---->1
- A window sells tickets---->8
- Window second sell ticket---->6
Why this kind of result is present. We might as well make a metaphor, actually just the procedure,
Inherit the thread class, we are equivalent to take out three things namely three tickets to sell 10 of the task divided to three windows, they each do the various things sold each of the tickets to complete each task, because Mythread inherits the thread class, so in new Mythread creates three threads while creating three objects;
Achieve runnable, equivalent to take out a ticket to sell 10 of the task to three people to work together, new mythread equivalent to create a task, and then instantiate three thread, create three threads to arrange three windows to execute.
The diagram shows the following:
We may be confused when we touch. Inherit the thread class and implement the Runnable interface to implement multi-threading, in fact, after contact we will find that this is completely two different implementations of multi-threading, one is multiple threads to complete their own tasks, one is multiple threads together to complete a task.
In fact, in the implementation of a task with multiple threads to do can also be used to inherit the thread class to achieve is just more trouble, generally we use the implementation of runnable interface to achieve, concise and clear.
In most cases, you should use the Runnable interface if you only want to override the run () method without overriding the other Thread methods. This is important because a subclass should not be created for the class (Thread) unless the programmer intends to modify or enhance the basic behavior of the class.
The difference between inheriting the thread class and implementing the Runnable interface in Java