자바 스 레 드 탱크 학습-2

3258 단어 자바thread
프로그램 을 썼 습 니 다.스 레 드 의 우선 순위 에 관 한 것 입 니 다.그러나 완 주 한 결 과 는 thinking in 자바 의 결론 과 다 릅 니 다.고수 가 지나 가 동생 에 게 지적 해 주 었 습 니 다.감사합니다.
package classes;

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


public class SimplePriorities implements Runnable{

	private int countDown=5;
	private volatile double d;
	private int priority;
	public SimplePriorities(int a){
		this.priority =a;
	}
	public String toString(){
		return Thread.currentThread()+":"+countDown;
	}
	@Override
	public void run() {
		Thread.currentThread().setPriority(priority);
		while(true){
			for (int i = 0; i < 10000; i++) {
				d+=(Math.PI+Math.E)/(double)i;
				if(i%1000==0)
					Thread.yield();
				System.out.println(this);
				if(--countDown==0)return;
			}
		}
	}
	public static void main(String[] args){
		ExecutorService exec = Executors.newCachedThreadPool();
		for (int i = 0; i < 5; i++) {
			exec.execute(new SimplePriorities(Thread.MIN_PRIORITY));
			exec.execute(new SimplePriorities(Thread.MAX_PRIORITY));
		}
		exec.shutdown();
	}
}
Thread[pool-1-thread-2,10,main]:5
Thread[pool-1-thread-2,10,main]:4
Thread[pool-1-thread-2,10,main]:3
Thread[pool-1-thread-2,10,main]:2
Thread[pool-1-thread-2,10,main]:1
Thread[pool-1-thread-2,10,main]:5
Thread[pool-1-thread-2,10,main]:4
Thread[pool-1-thread-2,10,main]:3
Thread[pool-1-thread-2,10,main]:2
Thread[pool-1-thread-2,10,main]:1
Thread[pool-1-thread-2,10,main]:5
Thread[pool-1-thread-2,10,main]:4
Thread[pool-1-thread-2,10,main]:3
Thread[pool-1-thread-2,10,main]:2
Thread[pool-1-thread-2,10,main]:1
Thread[pool-1-thread-2,10,main]:5
Thread[pool-1-thread-2,10,main]:4
Thread[pool-1-thread-2,10,main]:3
Thread[pool-1-thread-2,10,main]:2
Thread[pool-1-thread-2,10,main]:1
Thread[pool-1-thread-5,1,main]:5
Thread[pool-1-thread-5,1,main]:4
Thread[pool-1-thread-5,1,main]:3
Thread[pool-1-thread-5,1,main]:2
Thread[pool-1-thread-5,1,main]:1
Thread[pool-1-thread-6,1,main]:5
Thread[pool-1-thread-6,1,main]:4
Thread[pool-1-thread-6,1,main]:3
Thread[pool-1-thread-7,1,main]:5
Thread[pool-1-thread-7,1,main]:4
Thread[pool-1-thread-4,10,main]:5
Thread[pool-1-thread-3,1,main]:5
Thread[pool-1-thread-3,1,main]:4
Thread[pool-1-thread-3,1,main]:3
Thread[pool-1-thread-3,1,main]:2
Thread[pool-1-thread-3,1,main]:1
Thread[pool-1-thread-1,1,main]:5
Thread[pool-1-thread-4,10,main]:4
Thread[pool-1-thread-4,10,main]:3
Thread[pool-1-thread-4,10,main]:2
Thread[pool-1-thread-1,1,main]:4
Thread[pool-1-thread-1,1,main]:3
Thread[pool-1-thread-1,1,main]:2
Thread[pool-1-thread-1,1,main]:1
Thread[pool-1-thread-6,1,main]:2
Thread[pool-1-thread-6,1,main]:1
Thread[pool-1-thread-7,1,main]:3
Thread[pool-1-thread-4,10,main]:1
Thread[pool-1-thread-7,1,main]:2
Thread[pool-1-thread-7,1,main]:1

좋은 웹페이지 즐겨찾기