import java.util.*;

public class PriorityExample {
/**
 * argv[0]: number of increment operations per thread
*/
	public static void main(String argv[]) {	
		int maxPri = Thread.currentThread().getThreadGroup().getMaxPriority();
		Thread threads[] = new Thread[maxPri - Thread.MIN_PRIORITY+2];
				
		for (int i=Thread.MIN_PRIORITY; i<=maxPri; i++) {
			threads[i] = new Thread ( new CounterThread( new Counter(Long.parseLong(argv[0]))));
			threads[i].setPriority(i);
			threads[i].setName("pri"+i);
			} //for
		
		for (int i=Thread.MIN_PRIORITY; i<=maxPri; i++)
			threads[i].start();
	} //main()
} //class PriorityExample

class CounterThread implements Runnable {
	private Counter counter;
	private Counter max;
	
	public CounterThread(Counter max) {
		this.max = max;
		this.counter = new Counter();
	} //constructor
	
	public void run() {
		long start = System.currentTimeMillis();
		while (counter.get() < max.get())
			counter.increment();
		long end = System.currentTimeMillis() - start;
		System.out.println("Thread with priority "+Thread.currentThread().getPriority()+" took "+end+"ms");			
	} //run
} //class CounterThread	
			
class Counter {
	private long counter;
	
	public void increment() {
		System.out.println("INCREMENT by "+Thread.currentThread().getName());
		++counter;
	} //incremenet
	
	public long get() {
		return counter;
	} //get()
		
	public Counter() {
		this(0);
	} //constructor
	
	public Counter(long init) {
		counter = init;
	} //constructor
} //class Counter	
			