1. Via extends Thread
1 /*2 requirements: Simple to sell tickets, multiple windows at the same time to buy tickets3 (shared resources)4 second way to create a thread: Implementing the Runnable Interface5 6 steps:7 1, define class implementation runnable interface8 2, overwrite/override the Run method in the Runnable interface9 Store the code that the thread will run in the Run methodTen 3, thread objects are established through the thread class One 4, pass the subclass object of the Runnable interface as the actual parameter to the constructor of the thread class A why pass the subclass object of the Runnable interface to the thread's construction parameters? - A: The custom Run method (the custom code block is stored here) is a subclass object belonging to the Runnable interface - so to get the thread to specify the object's Run method, you must explicitly define the object that the Run method belongs to. the 5, call the Start method of the thread class to open the thread and invoke the subclass method of the Runnable interface - - the difference between implementation mode (implements) and inheritance (extends): - + benefits of the implementation approach: avoids the limitations of single inheritance - when defining a thread, it is best to use the implementation method + A two different ways to differentiate: at Inherited Threads: Thread code holds a thread subclass in the Run method - implement runnable: Thread code is stored in the Run method of the interface subclass - */ - classTicketextendsThread - { - Ticket (String name) in { - Super(name);//constructor: Set thread name to } + Private intTicket =100; - Public voidRun () the { * while(true) $ {Panax Notoginseng if(ticket>0) - { theSystem.out.println (Thread.CurrentThread (). GetName () + "more votes:" +ticket--); + } A } the } + } - classTicketdemo $ { $ Public Static voidMain (string[] args) - { -Ticket T1 =NewTicket ("Ticket window 1"); theTicket t2 =NewTicket ("Ticket window 2"); -Ticket t3 =NewTicket ("Ticket window 3");WuyiTicket T4 =NewTicket ("Ticket window 4"); the - T1.start (); Wu T2.start (); - T3.start (); About T4.start (); $System.out.println ("Hello world!"); - } -}
2, through implents Runnable:
classTicketImplementsRunnable//extends Thread{ Private intTicket =100; Public voidrun () { while(ticket>0) {System.out.println (Thread.CurrentThread (). GetName ()+ "Ticket count:" +ticket--); } }}classTicketdemo { Public Static voidMain (string[] args) {Ticket T=NewTicket (); Thread T1=NewThread (T, "ticket window 1"); Thread T2=NewThread (T, "ticket window 2"); Thread T3=NewThread (T, "ticket window 3"); Thread T4=NewThread (T, "ticket window 4"); T1.start (); T2.start (); T3.start (); T4.start (); System.out.println ("Hello world!"); }}
Comment Description:
1, no suprname, so delete.
2, you can see from the JDK manual that the thread constructor has this format:
Thread(Runnable target, String name)
Assigns a new Thread
object.
All, can be directly added to the object name
Error mode:
class Ticketdemo { publicstaticvoid main (string[] args) { = New Ticket (); New Thread (T, "ticket window 1"); T1.start (); T1.start (); T1.start (); T1.start (); System.out.println ("Hello world!" ); }}
This method causes the T1 thread to switch back from the running state, so the program error:
Exception in thread "main" java.lang.IllegalThreadStateException
At Java.lang.Thread.start (Unknown Source)
At Ticketdemo.main (ticketdemo.java:48)
Java multi-Thread sell tickets two ways