when a class has a father, but the functionality of which also wants to implement threads, then it is not possible to create threads in the same way as thread inheritance
Then you can do it through the interface.
Prepare to extend the functionality of the demo class so that its contents can be executed as a thread's task
Implement the Runnable interface, there is only one method in the Runnable interface run
The second way to create a thread
The advent of runnable is simply the encapsulation of the object of the thread's task
/*
* Second method of creating threads
* 1. Define Class implementation Runnable interface
* 2. Overwrite the fun method in the interface to encapsulate the thread's task code in the Run method
* 3. Create a thread object from the thread class and pass the subclass object of the Runnable interface as a parameter to the constructor
Why Because the threads ' tasks are encapsulated in the Run method of the Runnable interface subclass object, the thread object is created
* You need to be clear about the tasks you want to run
* 4. Call the thread's Start method to open the thread
*/
Class Demo implements Runnable{public void Run () {Show ();} public void Show () {for (int i = 0;i<10;i++) {System.out.println (Thread.CurrentThread (). GetName () + "i =" +i);}}} public class Main{public static void Main (string[] args) {//defines the thread's way to demo Jo = new demo ();//thread has a thread (Runnable T) in the method t Hread athread = new Thread (JO);//If not passed, start will only execute its own method Thread Bthread = new Thread (jo); Athread.start (); Bthread.start ();}}
Second, the realization of the Runnable interface and inherit the thread class difference
Benefits of implementing the Runnable interface:
1. Separate the thread's tasks from the thread's subclasses, and then encapsulate the task as an object
2. Avoid the limitations of single inheritance
Therefore, the second way to create threads is more common.
Third, the code example:
How the first thread is created
/* Required: 4 windows to buy tickets, 100 tickets, ref. 1-100 * */class Ticket extends thread{private int num = 100;//private static int num = 100;p ublic void Run () {while (true) {if (num>0) {System.out.println (Thread.CurrentThread (). GetName () + ". Sale. " +num--);}}} public class Main {public static void main (string[] args) {Ticket J1 = new Ticket (); Ticket J2 = new Ticket (); Ticket J3 = new Ticket (); Ticket J4 = new Ticket (); J1.start (); J2.start (); J3.start (); J4.start ();}}
PS: The first way to create a thread, there may be more than one window to sell the same number of tickets (No. 1th window sell 10th ticket, 3The number window is also for ticket No. 10th .)
of course, the ticket can be defined as static, but only for a ticket, if a variety of tickets is not applicable
The second way to create threads
/* Required: 4 windows to buy tickets, 100 tickets, ref. 1-100 * */class Ticket implements runnable{private int num = 100;//private static int num = 100;public void Run () {while (true) {if (num>0) {System.out.println (Thread.CurrentThread (). GetName () + ". Sale. " +num--);}}} public class Main {public static void main (string[] args) {/*ticket t = new Ticket ();//the act of selling tickets is encapsulated as an object Thread J1 = new Thread ( T); Thread J2 = new Thread (t); Thread J3 = new Thread (t); Thread J4 = new Thread (t); J1.start (); J2.start (); J3.start (); J4.start (); *///two kinds of tickets, arrangement, Ticket Zhanpiao = new Ticket (); Ticket Zuopiao = new Ticket ()//1 2 window sell arrangement, 3 4 window sell ticket Thread J1 = new Thread (Zhanpiao); Thread J2 = new Thread (Zhanpiao); Thread J3 = new Thread (Zuopiao); Thread J4 = new Thread (Zuopiao); J1.start (); J2.start (); J3.start (); J4.start ();}}
Iv. Thread Security issues
public void Run () {while (true) {if (num>0) {System.out.println (Thread.CurrentThread (). GetName () + ": Sale. " +num--);}}}
This code is a security risk, the ideal state will not appear to sell the No. 0 ticket, but once it occurs, the process will be an accident
Class Ticket implements runnable{private int num = 100;//private static int num = 100;public void run ()//cannot be throws here because Runn Able does not declare exception {while (true) {if (num>0) {///sleeps 10 milliseconds, sleep may have an exception//So can only be try,catch, cannot throw a try {thread.sleep];} catch ( Interruptedexception e) {//Todo:handle exception}system.out.println (Thread.CurrentThread (). GetName () + ". Sale. " +num--);}}} public class Main {public static void main (string[] args) {Ticket t = new Ticket (); Thread J1 = new Thread (t); Thread J2 = new Thread (t); Thread J3 = new Thread (t); Thread J4 = new Thread (t); J1.start (); J2.start (); J3.start (); J4.start ();}}
This is a security risk, so when you write multiple threads, you must consider security issues
Five, the cause of thread safety problems:
1. Multiple threads working on shared data (4 Windows operations shared num)
2. The thread code that operates the shared data has more than one
When a thread executes multiple code processes that share data, other threads participate in the operation, resulting in thread-safety problems
(For a simple example, the No. 1th window in the 1th ticket, not sold out, 2nd window will sell 1th tickets, this will appear to sell the case of No. 0 votes, if not try.) Direct selling usually does not happen)
Java Learning Lesson 23rd (Multithreading (ii))-(How to create multithreading two: Implement Runnable Interface (Common))