Thread class with runnable interface
1. Inherit the Thread class and overload the RUN method
script>
THREAD class: is a class that is designed to create threads and threads. Many methods are defined in Thread to operate threads. The Thread class is empty in the default. User threads can be implemented by inheriting the Thread class and override the RUN method of the Thread class. The overall structure is as follows: public class mythread extends thread {public void Run () {...}} mythread t = new mythread (); t. Start (); See example: twothreadstest.java twothreadstest.java: This example demonstrates how to pass Method for inheriting Thread implementation thread. This example simultaneously launched two threads. Please run, pay attention to the result of the result. Class twothreadstest {public static void main (string args []) {new simplethread ("jamaica"). Start (); new simplethread ("fiji"). Start ();}} Class SimpleThread Extends Thread {public simplethread (str) {super (STR);} public void run () {for (int i = 0; i <10; i ) {system.out.println (i "" getname ()) Try {Sleep ((int));} catch (interruptedException e) {}} system.out.println ("DONE!" getName ());}} In this example Two TWothReadStest's main () method constructed two SimpleThread class threads, one called "jamaica", the other is "FIJI", and then calls the start () method immediately after the construction is called to launch these two threads . Class SimpleThread is a subclass of THREAD, which first defines a construction method, where the argument is a string type, such as "Fiji" above, its role is to name the thread. The second method in class simplethread is the Run () method, which covers the Run () method in THREAD. A 10th cycle is a 10-time loop. The number of cycles is displayed in each cycle and the name of the currently running thread, and then sleeps a randomized time interval. After the cycle is over, "DONE!" And the thread name are displayed. Here we look at operating results:!! 0 Jamaica0 Fiji1 Fiji1 Jamaica2 Jamaica2 Fiji3 Fiji3 Jamaica4 Jamaica4 Fiji5 Jamaica5 Fiji6 Fiji6 Jamaica7 Jamaica7 Fiji8 Fiji9 Fiji8 JamaicaDONE Fiji9 JamaicaDONE Jamaica can be seen in the output names of the two threads are mixed together There is also no order, this is because the two threads are running simultaneously and are simultaneous display output.
2. Execute the class implementation RUN method
script>
Create a thread by establishing an object that implements the Runnable interface and uses it as a target object of the thread. Runnable interface: Define an abstract method Run (). Is defined as follows: public interface java.lang.Runnable {public abstract void run ();} general framework created as follows: class MyRunner implements Runnable {public void run () {...}} MyRunner r = new MyRunner (); Thread t = new Thread (ThreadGroup group, Runnable target, String name); see example: ThreadTesterer.javaThreadTest.java example: public class Clock extends java.applet.Appletimplements Runnable {Thread clockThread; public void start () {if (clockThread == null) {ClockThread = New Thread (this, "clock"); clockthread.start ();}} public void Run () {whele (clockthread! = null) {repaint (); try {clockthread.sleep (1000);} catch (InterruiptedException E) {}}} public void Paint (graphics g) {Date now = new date (); g.drawstring (now.getHours () ":" now.getminutes () ":" Now. GetSeconds (), 5, 10);} public void stop () {clockthread.stop (); clockthread = null;}} This is a clock applet, which displays the current time and updates in seconds. The applet provides a Run () method using the Runnable interface for its thread. Class Clock is extended by class Applet, but it requires a thread to implement time update display. Since multiple inherits are not supported in Java, it cannot inherit the THREAD, but use the Runnable interface. First, a thread called ClockThread is constructed and called the thread.start () method to start the thread, that is, the thread relationship of the system is established at Java runtime. Here's a new thread: ClockThread = New Thread (this, "Clock"); where this is the first argument in the Thread constructor, as the target object of the thread, it must implement the runnable interface. Under such a construction method, thread clockthread is its Run () method in its Run () method in its operable target object. In this example, this target object is a clock applet. The second variable in the constructor is a thread name. After the thread is started, the RUN () method of the target object is invoked unless the thread is stopped. In the cycle of the run () method, the Applet reloads itself, then sleeps for 1 second, and it is necessary to capture exception events and process it. If you leave this page of the display clock, the application will call the stop () method, set the line. When you return, create a new thread. In the specific application, which method is used to construct a line-in-line body to be dependent.
Typically, when a thread has inherited another class, it should be constructed in a second method, that is, the Runnable interface is implemented. 3. Summary:
script>
1. Both methods require the start method of the thread to allocate the system resources, scheduling threads run and execute the RUN method of the thread. 2. In the specific application, which method is used to construct a line-enforcement. Typically, when a thread has inherited another class, it should be constructed in a second method, that is, the Runnable interface is implemented. 3. The demise of threads is not recommended by calling a STOP () command. Instead, let the run () method ends naturally. See example: threadController.java
shut down