I'm attaching the ThreadTest file which I forgot to include in
my benchmark message.
Alexander
public class ThreadTest extends Thread {
public static final long MAX_COUNT = 1000000000;
public final char id;
public long counter = 0;
public ThreadTest peer = null;
public boolean isYield = false;
public boolean isNoop = false;
public ThreadTest(char id) {
super("Thread" + id);
this.id = id;
}
public void noop() {
}
public void setYield(boolean isYield) {
this.isYield = isYield;
}
public void setNoop(boolean isNoop) {
this.isNoop = isNoop;
}
public void setPeer(ThreadTest peer) {
this.peer = peer;
}
public void run() {
long startTime = System.currentTimeMillis();
while(counter < MAX_COUNT) {
if ( (isYield) && (counter % 1000 == 0) ) {
yield();
} else if ( (isNoop) && (counter % 1000 == 0) ) {
noop();
}
counter++;
}
System.out.print("Thread" + id + " " +
(System.currentTimeMillis() - startTime) + " ms");
if (peer != null) {
int percent = (int) ((((double) peer.counter) / MAX_COUNT) * 100);
System.out.print(" [peer counter=" + peer.counter + " (" +
percent + "%) ]");
}
System.out.println(" isYield=" + isYield + " isNoop=" + isNoop);
}
/**
* Usage:
*
* java ThreadTest
* java ThreadTest thread1
* java ThreadTest thread1 yield
* java ThreadTest thread2
* java ThreadTest thread2 yield
*/
public static void main(String[] args) {
ThreadTest thread1 = new ThreadTest('1');
ThreadTest thread2 = new ThreadTest('2');
thread1.setPeer(thread2);
thread2.setPeer(thread1);
boolean isYield = false;
boolean isNoop = false;
if (args.length > 1) {
if (args[1].equals("yield")) {
isYield = true;
} else if (args[1].equals("noop")) {
isNoop = true;
} else {
System.err.println("Unknown argument '" + args[1] + "'");
System.exit(1);
}
}
if (args.length == 0) {
// Same priority as main thread
} else if (args[0].equals("thread1")) {
thread2.setPriority(Thread.MIN_PRIORITY);
thread1.setYield(isYield);
thread1.setNoop(isNoop);
} else if (args[0].equals("thread2")) {
thread1.setPriority(Thread.MIN_PRIORITY);
thread2.setYield(isYield);
thread2.setNoop(isNoop);
} else {
System.err.println("Unknown argument '" + args[0] + "'");
System.exit(1);
}
thread1.start();
thread2.start();
}
}