Do an exercise: sell tickets to the small program, to achieve data sharing.
Let's start by creating the thread and running alternately with the main thread.
This is relatively simple:
In terms of inheriting the thread class:
Class MyThread extends thread{ private int num =; private String name; Public MyThread (String name) { super (name); } public void Run () {while (num >= 0) { System.out.println (Thread.CurrentThread (). GetName () + "... "+num--);}}} public class thread{public static void Main (string[] args) { MyThread a = new MyThread ("My"); MyThread B = new MyThread ("Your"); A.start (); B.start (); }}
With this example we can find that the value of NUM is duplicated, which is inconsistent with the phenomenon in our real life, for example: selling tickets.
The railway station sells tickets at the same time, but no duplicate tickets are found.
Create four threads, equivalent to four windows to sell tickets:
Class MyThread extends thread{ private int num =; private String name; Public MyThread (String name) { super (name); } public void Run () {while (num >= 0) { System.out.println (Thread.CurrentThread (). GetName () + " ... "+num--);}} } public class thread{public static void Main (string[] args) { MyThread a = new MyThread ("one"); MyThread B = new MyThread ("both"); MyThread C = new MyThread ("three"); MyThread d = new MyThread ("four"); A.start (); B.start (); C.start (); D.start (); }}
But the running results show that each ticket has been sold four times, if this happens in real life, then go home and can't buy tickets?
How to resolve.
The NUM variable is declared as static.
The data is defined as static, and data sharing is achieved:
Class MyThread extends thread{ private static int num =; private String name; Public MyThread (String name) { super (name); } public void Run () {while (num >= 0) { System.out.println (Thread.CurrentThread (). GetName () + "... "+num--);}}} public class thread{public static void Main (string[] args) { MyThread a = new MyThread ("one"); MyThread B = new MyThread ("both"); MyThread C = new MyThread ("three"); MyThread d = new MyThread ("four"); A.start (); B.start (); C.start (); D.start (); }}
The way to inherit the thread class has been to share data, but generally we do not define static because the static life cycle is too long.
Then how to implement the Runnable interface way?
We first create four threads in this way to run the thread.
Class MyThread implements runnable{ private static int num =; public void Run () {while (num >= 0) { System.out.println (Thread.CurrentThread (). GetName () + "..." +num--);}}} public class thread{public static void Main (string[] args) { MyThread a = new MyThread (); Thread a1 = new Thread (a); Thread a2 = new Thread (a); Thread a3 = new Thread (a); Thread a4 = new Thread (a); A1.start (); A2.start (); A3.start (); A4.start (); }}
A run we found that the ticket sales process has been implemented, has achieved data sharing. What's going on?
Why is it possible to implement the Runnable interface?
So first think about this question:
Why pass the subclass object of the Runnable interface as the actual parameter to the constructor of the thread class?
Because the object that the custom run method belongs to is the subclass object of the Runnable interface,
The Run method runs after the thread is started, but the new thread object does not have a replication run method.
The thread must have the Run method, but not itself, so be clear about the object to which the Run method belongs. is equivalent to the Run method of the borrowed Runnable subclass object.
So far, two ways of selling tickets have been achieved.
Java------multithreaded exercise: sell tickets---achieve data sharing