Java multithreading sets priority, java multithreading priority

Source: Internet
Author: User

Java multithreading sets priority, java multithreading priority

package com.itbuluoge.mythread;class SimpleThread extends Thread{private int priority;public SimpleThread(int i){priority=i;}public void run(){Thread.currentThread().setPriority(priority);for(int i=0;i<10000;i++){try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println(priority);}}}public class PriorityDemo {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubSimpleThread high=new SimpleThread(Thread.MAX_PRIORITY);SimpleThread low=new SimpleThread(Thread.MIN_PRIORITY);high.start();low.start();}}

As shown in the code above, the two running threads set the highest and lowest priority respectively. Let's look at the output result:

10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
1
10
1
10
10
1
10
1
1


It can be preliminarily found that the higher the priority, the higher the scheduling priority, but not very obvious.


JAVA multithreading priority

You are just lucky. In fact, set 5 to 2, 3, the probability of 4 is higher. Even if you set a higher priority, the CPU will still be allocated to the time slice of other threads, but the CPU computing speed is almost too fast. You can't imagine that you are counting within 50. if the number is less than 10000, you will feel it.
In fact, if you really want to implement this phenomenon, there is no need to set the priority. You can write t1.jion () in t2 so that you can implement it!
You don't have to worry about how the CPU is implemented at the underlying layer, but you need to know how the time slice is allocated. At least you need to know what it is, and you can understand many thread problems!

How to Set thread priority in java Multithreading

Java expert: sets priority for multi-threaded running. My computer is dual-core and cannot display the results normally. The thread priority indicates the importance of the thread. When multiple threads are in the executable status and wait
 

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.