码迷,mamicode.com
首页 > 编程语言 > 详细

【ThinkingInJava】59、执行线程的时候我们可以设定相应的优先级

时间:2015-05-23 10:08:20      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:java编程思想   线程   优先级   

/**
* 书本:《Thinking In Java》
* 功能:执行线程的时候我们可以设定相应的优先级
* 文件:SimplePriorities.java
* 时间:2015年5月7日15:17:09
* 作者:cutter_point
*/
package Lesson21Concurency;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SimplePriorities implements Runnable
{
	private int countDown = 5;
	//这里我们使用volatile时为了确保不进行任何编译器优化
	private volatile double d;	//volatile它是被设计用来修饰被不同线程访问和修改的变量,如果没有volatile,基本上会导致这样的结果:要么无法编写多线程程序,要么编译器失去大量优化的机会。
	private int priority;	//设定优先级
	
	public SimplePriorities(int priority)
	{
		this.priority = priority;
	}
	
	@Override
	public String toString()
	{
		return Thread.currentThread() + " : " + countDown;
	}


	@Override
	public void run()
	{
		Thread.currentThread().setPriority(priority);//设置线程的优先级
		//中间进行相应的一些计算
		while(true)
		{
			for(int i = 0; i < 100000; ++i)
			{
				d += (Math.PI + Math.E) / (double)i;
				if(i % 1000 == 0)
					Thread.yield();  //这个是表示线程可以更换了
			}
			System.out.println(this);		//输出当前任务的toString
			if(--countDown == 0)		//当计数减少到0的时候退出任务
				return;
		}
	}

	public static void main(String[] args)
	{
		ExecutorService exec = Executors.newCachedThreadPool();	//建立新的连接池
		
		for(int i = 0; i < 66; ++i)	//这里值要很据电脑的来设定,如果电脑性能比较好的话,可能就体现不出来优先级了,我的电脑大概在65到66左右才会完全体现5次的优先级
			exec.execute(new SimplePriorities(1));		//这里设定优先级不同的系统优先级个数不同,所以我们只用最大,最小,和中间的
		
		exec.execute(new SimplePriorities(10));		//这里设定优先级不同的系统优先级个数不同,所以我们只用最大,最小,和中间的
		exec.shutdown();
	}
}

输出:

Thread[pool-1-thread-67,10,main] : 5
Thread[pool-1-thread-67,10,main] : 4
Thread[pool-1-thread-67,10,main] : 3
Thread[pool-1-thread-67,10,main] : 2
Thread[pool-1-thread-67,10,main] : 1
Thread[pool-1-thread-2,1,main] : 5
Thread[pool-1-thread-4,1,main] : 5
Thread[pool-1-thread-7,1,main] : 5
Thread[pool-1-thread-10,1,main] : 5
Thread[pool-1-thread-27,1,main] : 5
Thread[pool-1-thread-11,1,main] : 5
Thread[pool-1-thread-18,1,main] : 5
Thread[pool-1-thread-15,1,main] : 5
Thread[pool-1-thread-14,1,main] : 5
Thread[pool-1-thread-19,1,main] : 5
Thread[pool-1-thread-22,1,main] : 5
Thread[pool-1-thread-23,1,main] : 5
Thread[pool-1-thread-31,1,main] : 5
Thread[pool-1-thread-30,1,main] : 5
Thread[pool-1-thread-26,1,main] : 5
Thread[pool-1-thread-3,1,main] : 5
Thread[pool-1-thread-34,1,main] : 5
Thread[pool-1-thread-38,1,main] : 5
Thread[pool-1-thread-35,1,main] : 5
Thread[pool-1-thread-1,1,main] : 5
Thread[pool-1-thread-39,1,main] : 5
Thread[pool-1-thread-42,1,main] : 5
Thread[pool-1-thread-46,1,main] : 5
Thread[pool-1-thread-43,1,main] : 5
Thread[pool-1-thread-50,1,main] : 5
Thread[pool-1-thread-5,1,main] : 5
Thread[pool-1-thread-51,1,main] : 5
Thread[pool-1-thread-6,1,main] : 5
Thread[pool-1-thread-9,1,main] : 5
Thread[pool-1-thread-55,1,main] : 5
Thread[pool-1-thread-47,1,main] : 5
Thread[pool-1-thread-54,1,main] : 5
Thread[pool-1-thread-58,1,main] : 5
Thread[pool-1-thread-59,1,main] : 5
Thread[pool-1-thread-66,1,main] : 5
Thread[pool-1-thread-62,1,main] : 5
Thread[pool-1-thread-8,1,main] : 5
Thread[pool-1-thread-12,1,main] : 5
Thread[pool-1-thread-13,1,main] : 5
Thread[pool-1-thread-63,1,main] : 5
Thread[pool-1-thread-17,1,main] : 5
Thread[pool-1-thread-21,1,main] : 5
Thread[pool-1-thread-24,1,main] : 5
Thread[pool-1-thread-28,1,main] : 5
Thread[pool-1-thread-20,1,main] : 5
Thread[pool-1-thread-29,1,main] : 5
Thread[pool-1-thread-16,1,main] : 5
Thread[pool-1-thread-25,1,main] : 5
Thread[pool-1-thread-33,1,main] : 5
Thread[pool-1-thread-41,1,main] : 5
Thread[pool-1-thread-57,1,main] : 5
Thread[pool-1-thread-32,1,main] : 5
Thread[pool-1-thread-44,1,main] : 5
Thread[pool-1-thread-40,1,main] : 5
Thread[pool-1-thread-60,1,main] : 5
Thread[pool-1-thread-56,1,main] : 5
Thread[pool-1-thread-53,1,main] : 5
Thread[pool-1-thread-45,1,main] : 5
Thread[pool-1-thread-52,1,main] : 5
Thread[pool-1-thread-64,1,main] : 5
Thread[pool-1-thread-49,1,main] : 5
Thread[pool-1-thread-48,1,main] : 5
Thread[pool-1-thread-36,1,main] : 5
Thread[pool-1-thread-37,1,main] : 5
Thread[pool-1-thread-61,1,main] : 5
Thread[pool-1-thread-65,1,main] : 5
Thread[pool-1-thread-2,1,main] : 4
Thread[pool-1-thread-7,1,main] : 4
Thread[pool-1-thread-27,1,main] : 4
Thread[pool-1-thread-10,1,main] : 4
Thread[pool-1-thread-11,1,main] : 4
Thread[pool-1-thread-14,1,main] : 4
Thread[pool-1-thread-18,1,main] : 4
Thread[pool-1-thread-15,1,main] : 4
Thread[pool-1-thread-23,1,main] : 4
Thread[pool-1-thread-26,1,main] : 4
Thread[pool-1-thread-31,1,main] : 4
Thread[pool-1-thread-34,1,main] : 4
Thread[pool-1-thread-30,1,main] : 4
Thread[pool-1-thread-46,1,main] : 4
Thread[pool-1-thread-39,1,main] : 4
Thread[pool-1-thread-35,1,main] : 4
Thread[pool-1-thread-42,1,main] : 4
Thread[pool-1-thread-19,1,main] : 4
Thread[pool-1-thread-43,1,main] : 4
Thread[pool-1-thread-58,1,main] : 4
Thread[pool-1-thread-6,1,main] : 4
Thread[pool-1-thread-51,1,main] : 4
Thread[pool-1-thread-22,1,main] : 4
Thread[pool-1-thread-62,1,main] : 4
Thread[pool-1-thread-59,1,main] : 4
Thread[pool-1-thread-55,1,main] : 4
Thread[pool-1-thread-54,1,main] : 4
Thread[pool-1-thread-66,1,main] : 4
Thread[pool-1-thread-38,1,main] : 4
Thread[pool-1-thread-50,1,main] : 4
Thread[pool-1-thread-63,1,main] : 4
Thread[pool-1-thread-3,1,main] : 4
Thread[pool-1-thread-47,1,main] : 4
Thread[pool-1-thread-4,1,main] : 4
Thread[pool-1-thread-1,1,main] : 4
Thread[pool-1-thread-5,1,main] : 4
Thread[pool-1-thread-9,1,main] : 4
Thread[pool-1-thread-13,1,main] : 4
Thread[pool-1-thread-12,1,main] : 4
Thread[pool-1-thread-8,1,main] : 4
Thread[pool-1-thread-17,1,main] : 4
Thread[pool-1-thread-28,1,main] : 4
Thread[pool-1-thread-29,1,main] : 4
Thread[pool-1-thread-24,1,main] : 4
Thread[pool-1-thread-25,1,main] : 4
Thread[pool-1-thread-20,1,main] : 4
Thread[pool-1-thread-33,1,main] : 4
Thread[pool-1-thread-16,1,main] : 4
Thread[pool-1-thread-57,1,main] : 4
Thread[pool-1-thread-41,1,main] : 4
Thread[pool-1-thread-32,1,main] : 4
Thread[pool-1-thread-60,1,main] : 4
Thread[pool-1-thread-40,1,main] : 4
Thread[pool-1-thread-45,1,main] : 4
Thread[pool-1-thread-44,1,main] : 4
Thread[pool-1-thread-36,1,main] : 4
Thread[pool-1-thread-48,1,main] : 4
Thread[pool-1-thread-53,1,main] : 4
Thread[pool-1-thread-64,1,main] : 4
Thread[pool-1-thread-49,1,main] : 4
Thread[pool-1-thread-61,1,main] : 4
Thread[pool-1-thread-56,1,main] : 4
Thread[pool-1-thread-65,1,main] : 4
Thread[pool-1-thread-52,1,main] : 4
Thread[pool-1-thread-37,1,main] : 4
Thread[pool-1-thread-21,1,main] : 4
Thread[pool-1-thread-2,1,main] : 3
Thread[pool-1-thread-7,1,main] : 3
Thread[pool-1-thread-27,1,main] : 3
Thread[pool-1-thread-10,1,main] : 3
Thread[pool-1-thread-11,1,main] : 3
Thread[pool-1-thread-14,1,main] : 3
Thread[pool-1-thread-18,1,main] : 3
Thread[pool-1-thread-15,1,main] : 3
Thread[pool-1-thread-23,1,main] : 3
Thread[pool-1-thread-26,1,main] : 3
Thread[pool-1-thread-31,1,main] : 3
Thread[pool-1-thread-34,1,main] : 3
Thread[pool-1-thread-30,1,main] : 3
Thread[pool-1-thread-39,1,main] : 3
Thread[pool-1-thread-42,1,main] : 3
Thread[pool-1-thread-35,1,main] : 3
Thread[pool-1-thread-19,1,main] : 3
Thread[pool-1-thread-46,1,main] : 3
Thread[pool-1-thread-43,1,main] : 3
Thread[pool-1-thread-51,1,main] : 3
Thread[pool-1-thread-58,1,main] : 3
Thread[pool-1-thread-55,1,main] : 3
Thread[pool-1-thread-6,1,main] : 3
Thread[pool-1-thread-22,1,main] : 3
Thread[pool-1-thread-62,1,main] : 3
Thread[pool-1-thread-66,1,main] : 3
Thread[pool-1-thread-59,1,main] : 3
Thread[pool-1-thread-54,1,main] : 3
Thread[pool-1-thread-38,1,main] : 3
Thread[pool-1-thread-50,1,main] : 3
Thread[pool-1-thread-63,1,main] : 3
Thread[pool-1-thread-3,1,main] : 3
Thread[pool-1-thread-47,1,main] : 3
Thread[pool-1-thread-4,1,main] : 3
Thread[pool-1-thread-1,1,main] : 3
Thread[pool-1-thread-5,1,main] : 3
Thread[pool-1-thread-13,1,main] : 3
Thread[pool-1-thread-9,1,main] : 3
Thread[pool-1-thread-17,1,main] : 3
Thread[pool-1-thread-12,1,main] : 3
Thread[pool-1-thread-8,1,main] : 3
Thread[pool-1-thread-28,1,main] : 3
Thread[pool-1-thread-57,1,main] : 3
Thread[pool-1-thread-29,1,main] : 3
Thread[pool-1-thread-24,1,main] : 3
Thread[pool-1-thread-25,1,main] : 3
Thread[pool-1-thread-20,1,main] : 3
Thread[pool-1-thread-33,1,main] : 3
Thread[pool-1-thread-41,1,main] : 3
Thread[pool-1-thread-16,1,main] : 3
Thread[pool-1-thread-32,1,main] : 3
Thread[pool-1-thread-45,1,main] : 3
Thread[pool-1-thread-40,1,main] : 3
Thread[pool-1-thread-60,1,main] : 3
Thread[pool-1-thread-44,1,main] : 3
Thread[pool-1-thread-53,1,main] : 3
Thread[pool-1-thread-36,1,main] : 3
Thread[pool-1-thread-48,1,main] : 3
Thread[pool-1-thread-64,1,main] : 3
Thread[pool-1-thread-49,1,main] : 3
Thread[pool-1-thread-56,1,main] : 3
Thread[pool-1-thread-61,1,main] : 3
Thread[pool-1-thread-52,1,main] : 3
Thread[pool-1-thread-65,1,main] : 3
Thread[pool-1-thread-37,1,main] : 3
Thread[pool-1-thread-21,1,main] : 3
Thread[pool-1-thread-2,1,main] : 2
Thread[pool-1-thread-7,1,main] : 2
Thread[pool-1-thread-27,1,main] : 2
Thread[pool-1-thread-10,1,main] : 2
Thread[pool-1-thread-14,1,main] : 2
Thread[pool-1-thread-11,1,main] : 2
Thread[pool-1-thread-18,1,main] : 2
Thread[pool-1-thread-23,1,main] : 2
Thread[pool-1-thread-15,1,main] : 2
Thread[pool-1-thread-26,1,main] : 2
Thread[pool-1-thread-31,1,main] : 2
Thread[pool-1-thread-34,1,main] : 2
Thread[pool-1-thread-30,1,main] : 2
Thread[pool-1-thread-42,1,main] : 2
Thread[pool-1-thread-43,1,main] : 2
Thread[pool-1-thread-39,1,main] : 2
Thread[pool-1-thread-35,1,main] : 2
Thread[pool-1-thread-19,1,main] : 2
Thread[pool-1-thread-46,1,main] : 2
Thread[pool-1-thread-58,1,main] : 2
Thread[pool-1-thread-51,1,main] : 2
Thread[pool-1-thread-55,1,main] : 2
Thread[pool-1-thread-6,1,main] : 2
Thread[pool-1-thread-62,1,main] : 2
Thread[pool-1-thread-22,1,main] : 2
Thread[pool-1-thread-66,1,main] : 2
Thread[pool-1-thread-59,1,main] : 2
Thread[pool-1-thread-54,1,main] : 2
Thread[pool-1-thread-38,1,main] : 2
Thread[pool-1-thread-50,1,main] : 2
Thread[pool-1-thread-63,1,main] : 2
Thread[pool-1-thread-3,1,main] : 2
Thread[pool-1-thread-47,1,main] : 2
Thread[pool-1-thread-4,1,main] : 2
Thread[pool-1-thread-1,1,main] : 2
Thread[pool-1-thread-5,1,main] : 2
Thread[pool-1-thread-13,1,main] : 2
Thread[pool-1-thread-17,1,main] : 2
Thread[pool-1-thread-12,1,main] : 2
Thread[pool-1-thread-9,1,main] : 2
Thread[pool-1-thread-8,1,main] : 2
Thread[pool-1-thread-57,1,main] : 2
Thread[pool-1-thread-28,1,main] : 2
Thread[pool-1-thread-20,1,main] : 2
Thread[pool-1-thread-25,1,main] : 2
Thread[pool-1-thread-24,1,main] : 2
Thread[pool-1-thread-29,1,main] : 2
Thread[pool-1-thread-33,1,main] : 2
Thread[pool-1-thread-41,1,main] : 2
Thread[pool-1-thread-16,1,main] : 2
Thread[pool-1-thread-32,1,main] : 2
Thread[pool-1-thread-45,1,main] : 2
Thread[pool-1-thread-40,1,main] : 2
Thread[pool-1-thread-60,1,main] : 2
Thread[pool-1-thread-64,1,main] : 2
Thread[pool-1-thread-36,1,main] : 2
Thread[pool-1-thread-61,1,main] : 2
Thread[pool-1-thread-53,1,main] : 2
Thread[pool-1-thread-49,1,main] : 2
Thread[pool-1-thread-56,1,main] : 2
Thread[pool-1-thread-44,1,main] : 2
Thread[pool-1-thread-37,1,main] : 2
Thread[pool-1-thread-52,1,main] : 2
Thread[pool-1-thread-48,1,main] : 2
Thread[pool-1-thread-65,1,main] : 2
Thread[pool-1-thread-21,1,main] : 2
Thread[pool-1-thread-2,1,main] : 1
Thread[pool-1-thread-7,1,main] : 1
Thread[pool-1-thread-27,1,main] : 1
Thread[pool-1-thread-10,1,main] : 1
Thread[pool-1-thread-14,1,main] : 1
Thread[pool-1-thread-11,1,main] : 1
Thread[pool-1-thread-18,1,main] : 1
Thread[pool-1-thread-15,1,main] : 1
Thread[pool-1-thread-23,1,main] : 1
Thread[pool-1-thread-26,1,main] : 1
Thread[pool-1-thread-31,1,main] : 1
Thread[pool-1-thread-34,1,main] : 1
Thread[pool-1-thread-30,1,main] : 1
Thread[pool-1-thread-43,1,main] : 1
Thread[pool-1-thread-42,1,main] : 1
Thread[pool-1-thread-39,1,main] : 1
Thread[pool-1-thread-35,1,main] : 1
Thread[pool-1-thread-19,1,main] : 1
Thread[pool-1-thread-46,1,main] : 1
Thread[pool-1-thread-51,1,main] : 1
Thread[pool-1-thread-55,1,main] : 1
Thread[pool-1-thread-58,1,main] : 1
Thread[pool-1-thread-6,1,main] : 1
Thread[pool-1-thread-62,1,main] : 1
Thread[pool-1-thread-22,1,main] : 1
Thread[pool-1-thread-66,1,main] : 1
Thread[pool-1-thread-59,1,main] : 1
Thread[pool-1-thread-50,1,main] : 1
Thread[pool-1-thread-54,1,main] : 1
Thread[pool-1-thread-38,1,main] : 1
Thread[pool-1-thread-63,1,main] : 1
Thread[pool-1-thread-3,1,main] : 1
Thread[pool-1-thread-28,1,main] : 1
Thread[pool-1-thread-48,1,main] : 1
Thread[pool-1-thread-20,1,main] : 1
Thread[pool-1-thread-44,1,main] : 1
Thread[pool-1-thread-60,1,main] : 1
Thread[pool-1-thread-16,1,main] : 1
Thread[pool-1-thread-65,1,main] : 1
Thread[pool-1-thread-36,1,main] : 1
Thread[pool-1-thread-9,1,main] : 1
Thread[pool-1-thread-12,1,main] : 1
Thread[pool-1-thread-61,1,main] : 1
Thread[pool-1-thread-21,1,main] : 1
Thread[pool-1-thread-1,1,main] : 1
Thread[pool-1-thread-33,1,main] : 1
Thread[pool-1-thread-25,1,main] : 1
Thread[pool-1-thread-24,1,main] : 1
Thread[pool-1-thread-53,1,main] : 1
Thread[pool-1-thread-52,1,main] : 1
Thread[pool-1-thread-37,1,main] : 1
Thread[pool-1-thread-40,1,main] : 1
Thread[pool-1-thread-4,1,main] : 1
Thread[pool-1-thread-47,1,main] : 1
Thread[pool-1-thread-41,1,main] : 1
Thread[pool-1-thread-49,1,main] : 1
Thread[pool-1-thread-13,1,main] : 1
Thread[pool-1-thread-56,1,main] : 1
Thread[pool-1-thread-29,1,main] : 1
Thread[pool-1-thread-17,1,main] : 1
Thread[pool-1-thread-5,1,main] : 1
Thread[pool-1-thread-8,1,main] : 1
Thread[pool-1-thread-32,1,main] : 1
Thread[pool-1-thread-64,1,main] : 1
Thread[pool-1-thread-45,1,main] : 1
Thread[pool-1-thread-57,1,main] : 1





【ThinkingInJava】59、执行线程的时候我们可以设定相应的优先级

标签:java编程思想   线程   优先级   

原文地址:http://blog.csdn.net/cutter_point/article/details/45932089

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!