Anyway, I went into Eclipse on my Linux box (from which I am posting this), and in no time at all, I was able to write a trivial little Java application to refresh my memory of how this stuff works.
public class MultipleThreads {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TestThread count1 = new TestThread(1, 10, 500);
count1.start();
while (count1.getCalcCount() < 5)
;
TestThread count2 = new TestThread(1000, 1010, 250);
count2.start();
}
}
public class TestThread extends Thread {
private int start;
private int last;
private int delayInterval;
private int calcCount;
public int getCalcCount() {
return calcCount;
}
public void setCalcCount(int calcCount) {
this.calcCount = calcCount;
}
TestThread(int begin, int end, int delay)
{
this.start = begin;
this.last = end;
this.delayInterval = delay;
calcCount = 0;
}
public void run() {
for (int i = start; i < last; i++)
{
System.out.println("sqrt(" + i + ")=" + Math.sqrt((double)i));
calcCount++;
try {
sleep(delayInterval);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
It does not anything terribly significant; I was just verifying that I knew how to create an application with two threads that could interact with the main application thread. The cool thing is that Eclipse (which is open source) is so easy to use that I could throw this together in just a couple of minutes and get it working.
No comments:
Post a Comment