首页 > 代码库 > java多线程设置优先级

java多线程设置优先级

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 block
				e.printStackTrace();
			}
			
			System.out.println(priority);
		}
	}
}
public class PriorityDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SimpleThread 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


能够初步发现。优先级高的,得到的调度会更优先一些。可是也不是很明显。

java多线程设置优先级