线程优先级

import java.io.*;

public class PriorityTest {
	static int NUM_T = 4;
	static boolean yield = true;
	static int[] counter =new int[NUM_T];
	public static void main(String[] args) {
		PrintWriter out = new PrintWriter(System.out, true);
		int numIntervals = 10;
		if(args.length > 0) {
			yield = false;
		}
		out.println("Using yield() ? " + (yield ? "Yes " : "No "));
		for (int i = 0; i<NUM_T; i++) {
			(new prTestThread ((i > 1) ? 4 : (i + 1), i)).start();
		}
		ThreadInfo.printAllThreadInfo();
		out.println();
		int step = 0;
		while (true ) {
			boolean allDone = true;
			try {
				Thread.sleep (300);
			}catch(InterruptedException e) {}
			out.print("Step " + (step ++) + ":  COUNTERS:");
			for(int j = 0; j<NUM_T; j++) {
				out.print( "  " + counter[j]);
				if (counter[j] < 2000000) {
					allDone = false;	
				}
			}
			System.exit(0);
		}
	}
}
class prTestThread extends Thread {
	int id;
	prTestThread (int priority, int id) {
		super("prTestThread #" + id);
		this.id = id;
		setPriority(priority);
	}
	public void run() {
		for(int i = 0; i <= 2000000; i++) {
			if(((i % 3000) == 0) && PriorityTest.yield) {
				yield();
			}
			PriorityTest.counter[id] = i;
		}
	}
}

 

你可能感兴趣的:(thread,J#)