Java Threading Control methods

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.