Java 22-6 Multi-thread threads scheduling and setting the priority of threads

Source: Internet
Author: User

Thread scheduling 

If our computer has only one CPU, then the CPU can only execute one instruction at a time, and the thread only gets the CPU time slice, that is, the right to use, to execute the instruction.

So how does Java make calls to threads?

There are two scheduling models for threads :

Time- sharing scheduling model All threads take turns using the CPU's right of use, evenly allocating each thread to occupy the CPU of the chip

preemptive scheduling models prioritize high-priority threads to use the CPU, and if the threads have the same priority, they randomly select a higher-priority thread to get more CPU time slices.

Java uses a preemptive scheduling model.

Now that the thread has a set priority, there must be a default priority.
So what is the default priority? How do I get the priority of a thread object?
  Public final int getpriority (): Returns the priority of the thread object
How do I set the priority of a thread object?
  Public final void setpriority (int newpriority): Change the priority of the thread.

Attention:
The thread default priority is 5.
The range of thread priorities is: 1-10.
High thread priority only indicates that the thread gets a high chance of CPU time slices, but only when the number of times is higher, or multiple runs, can you see the better effect.

IllegalArgumentException: Illegal parameter exception.
The exception thrown indicates that an invalid or incorrect parameter was passed to the method. Is that the priority of the setting is outside the scope of the thread priority.

Get the priority of the thread, view the default thread priority of the system, and set the thread priority:

1      Public Static voidMain (string[] args) {2 3         //Create two Thread objects4MyThread MT1 =NewMyThread ();5MyThread mt2 =NewMyThread ();6MyThread MT3 =NewMyThread ();7         8         //call method to set thread name9Mt1.setname ("Husky");TenMt2.setname ("Samoyed"); OneMt3.setname ("Short-tailed Cat")); A          -         //getting the default priority result is 5, proving that the system default thread priority is 5 - System.out.println (Mt1.getpriority ()); the System.out.println (Mt2.getpriority ()); - System.out.println (Mt3.getpriority ()); -          -         //Set Thread Priority +         //the higher the number, the greater the priority. However, a higher priority simply means that the thread gets a relatively high number of CPU time slices. It's not that I'm going to let you finish.  -Mt1.setpriority (1); +Mt2.setpriority (6); AMt3.setpriority (10); at          -          - Mt1.start (); - Mt2.start (); - Mt3.start (); -          in}

Java 22-6 Multi-thread threads scheduling and setting the priority of threads

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.