1. The second way to implement threading
Provides a class that implements the interface runnable as the target object of the thread, when initializing a thread object of a thread class, or threads subclass, to pass the target object to this thread instance, provided by the target object to the thread body
Class Runnableimpl implements Runnable{public void Run () {for (int i = 0; i <, i + +;) {System.out.println ("Runnable --"+ i);}}}
Class Test{public static void Main (String args[]) {//generates an object for the Runnable interface implementation class Runnableimpl RI = new Runnableimpl ();// Generates a thread object and passes the object of the Runnable interface implementation class as a parameter to the thread object thread t = new thread (),//Notifies the thread object, executes the Start Method T.start ();}}
In the actual development process more inclined to the second approach, because the first inheritance thread has only one chance, inheritance thread can not inherit other (single inheritance)
2. Common methods for controlling threads
。 Break Thread in
--thread.sleep (); Thread.Sleep (2000); After sleep in the thread body to set a parameter (2000 milliseconds) sleep (2000) after a short period of time to run, because the thread to preempt the CPU
Test:
Class Runnableimpl implements Runnable{public void Run () {for (int i = 0; i <, i + +;) {System.out.println ("Runnable --"+ i), if (i = =) {try{thread.sleep (2000);} catch (Exception e) {System.out.println (e);}}}}
Class Test{public static void Main (String args[]) {//generates an object for the Runnable interface implementation class Runnableimpl RI = new Runnableimpl ();// Generates a thread object and passes the object of the Runnable interface implementation class as a parameter to the thread object thread t = new thread (),//Notifies the thread object, executes the Start Method T.start ();}}
--thread.yield (); Execute to this line automatically out of the CPU, and then two threads at the same time preemption CPU, it is possible or this thread to grab the CPU
。 Set the priority of a thread
--getpriority ();
--setpriority ();
Class Test{public static void Main (String args[]) {//generates an object for the Runnable interface implementation class Runnableimpl RI = new Runnableimpl ();// Generates a thread object and passes the object of the Runnable interface implementation class as a parameter to the thread object//The thread has a maximum priority of 10, the minimum is 1, and the thread's priority can be set using the static constants provided by thread;//<span style= " Color: #ff0000; " > Note: The higher the priority of the thread, the greater the probability of execution (note that probability is not necessarily performed first) </span>//t.setpriority (thread.max_priority);// Set maximum priority t.setpriority (thread.min_priority),//Set minimum priority Thread T = new thread (),//Notify thread object, execute Start Method T.start (); System.out.println (T.getpriority ());}}
Threads in Java (ii)