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