One, medium disconnection process
1.thread.sleep ()
Let the thread go to sleep and discard the CPU for a pause of several milliseconds
How to use:
public class Runable implements Runnable {@Overridepublic void Run () {for (int i=1;i<100;i++) {System.out.println (" First runnable--> "+i"; try {thread.sleep (200);//Set sleep time to 200 milliseconds,} catch (Interruptedexception e) {//TODO Auto-generated catch Blocke.printstacktrace ();}}}
2.THREAD.YIDLD ()
Let the thread discard the CPU, but continue to preempt the CPU
Second, set the priority of the thread
The default priority is 5, the maximum priority is 10, and the minimum priority is 1
1.getPriority ()
Get the priority of the current process
2.setPriority ()
Sets the priority of the current thread
How to use:
public class Main {public static void main (string[] args) throws Interruptedexception {//Implement Interface runable ra=new runable ();//Generate The thread object and the interface object as the parameter thread t=new thread (RA); the//sleep () method uses T.sleep (200);//Gets the current priority int temp=t.getpriority (); SYSTEM.OUT.PRINTLN ("Default priority:" +temp);//Set highest priority and output t.setpriority (thread.max_priority); temp=t.getpriority (); System.out.println ("Highest priority:" +temp);//Set the lowest priority and output t.setpriority (thread.min_priority); temp=t.getpriority (); SYSTEM.OUT.PRINTLN ("Lowest priority:" +temp);}}
Java Threading Control methods