java多线程设置优先级

来源:互联网 发布:淘宝网发货地址怎么改 编辑:程序博客网 时间:2024/05/16 11:37
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();}}

如上述代码所示,运行的两个线程,分别设置最高和最低的优先级,我们可以看看输出结果:

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


可以初步发现,优先级高的,得到的调度会更优先一些,但是也不是非常明显。

0 0
原创粉丝点击