Two ways to create thread 1, extends thread this class. 2, implements runnable this interface.
Set Daemon (); Create a background process; Tt.join ("a") means that a thread is released after it is added to the TT (a) MS;
Advantages of using runnable:
Optimize the single inheritance of Java language;
Suitable for multiple threads using the same resource situation;
Train ticket window A total of 100 tickets, with the threading simulation;
Usage:
Class threaddemo{public static void Main (string[] args) { testthread tt=new testthread ();//enable multiple threads, Multi-threaded shared resources; new Thread (TT). Start (); New Thread (TT). Start (); New Thread (TT). Start (); New Thread (TT). Start (); }} Class Testthread implements runnable{ int tickets=100; public void Run () {while (true) { //system.out.println ("Run ():" +thread.currentthread (). GetName ()); while (tickets-->0) System.out.println (Thread.CurrentThread (). GetName () + "is sailing ticket" + tickets--); } }}
Class threaddemo{public static void Main (string[] args) { new Testthread (). Start (); Enable multiple threads, each thread resource independent; new Testthread (). Start (); New Testthread (). Start (); New Testthread (). Start (); /*testthread tt=new testthread (); Tt.start (); Tt.start (); Tt.start (); Tt.start (); */ }}class testthread extends thread{ int tickets=100; public void Run () {while (true) { //system.out.println ("Run ():" +thread.currentthread (). GetName ()); while (tickets-->0) System.out.println (Thread.CurrentThread (). GetName () + "is sailing ticket" + tickets--); } }}
Java--Threading