1. Multi-thread 1.1 Creating a thread class in Java can simply inherit from the Thread class Create your own thread class: public class myfirstthread extends thread {public void Run () {..}} Description: (1) THREAD class It is a java.lang package, so you don't have to display import; (2) It is best to override the Run () method inherited from the THREAD class to run the required code; you can instantiate and run the thread as follows: MyFirstthread Amft = new myfirstthread (); amft.start (); Description: (3) After instanting the thread class, the system will initialize some parameters, mainly to create a name for threads, add new threads to the specified thread group, initialize thread operation The required memory space specifies the priority level of the new thread, specifies its wait thread; (4) START method is the method in the Thread class, which calls the RUN method, runs the specified code in the new thread; (5) except In addition to the START method, the classes from Thread also have some other major methods: STOP, SUSPEND, RESUME, etc .; the following is a complete Thread derived class: 1: public class complexthread extends thread {2: private int delay; 3: 4: ComplexThread (String Name, Float Seconds) {5: Super (Name); 6: Delay = (int) seconds * 1000; // delays are in milliseconds 7: start (); // start up ourself! 8:} 9:10: Public void Run () {11: while (true) {12: system.out.println (thread.currentthread (). GetName ()); 13: try {14: thread.sleep (delay); 15 :} Catch (Inter) RuptedException E) {16: Return; 17:} 18:} 19:} 20:21: public static void main (string argv []) {22: New ComplexThread ("One Potato", 1.1F); 23: New ComplexThread ("Two Potato", 1.3F); 24: New ComplexThread ("Three Potato", 0.5F); 25: New ComplexThread ("FOUR", 0.7F); 26:} 27:} 1.2 Runable interface creates multi-thread operation Another way to specify the code is that the interface of the Implement Runable:
Public class mysecondthread extends ImportantClass Implements Runnable {public void Run () {..}} Description: (1) This type of Implement runable interface, indicating that intention to run in a separate thread, Thread is also an Implement Runable interface; (2 The Implement Runalbe interface requires at least the RUN method; the following is an example of creating a new thread running the class: Mysecondthread Amst = new mysecondthread (); thread athread = new thread (Athread.Start (); Description: (3) Thread There are multiple constructor thread (), thread (Runable Target), etc., this example is the second constructor, which has a runable function, so the class to be run in the multi-thread must be Implement runable; (4) AtHead.Start () method calls the Target.Run method in the Thread instance, this example is the RUN method in the MysecondThread instance; (5) The Thread constructor can also specify the thread name and run the required STACK, the group to which the thread belongs is, etc., in order to prevent the thread from normal end, you need to put the start method into try ... caratch, such as: try {mythread.start (); Catch (threaddeath ATD) {system.out.println "End thread"); throw att;} In this example, the THREADDEATH is abnormal, and the abnormality will be re-thus processed, so that Java executes the STOP method to clean up the resources. 1.3 Priority of threads Multiple threads have a certain priority, for the following example: public class runnablepotato imports runnable {public void Run () {While (true) system.out.println (thread.currentthread () .getName ());}} public class PotatoThreadTester {public static void main (String argv []) {RunnablePotato aRP = new RunnablePotato (); Thread T1 = new Thread (aRP, "one potato"); Thread T2 = new Thread (ARP, "Two Potato"); T1.Start (); t2.start ();}} For non-seizuated systems, the first thread in the above example has been running, and the second thread does not have the opportunity to run; For the preemptive system, this two-person thread will run alternately.