Thursday, May 5, 2011

Eclipse

I have to brush up my Java threads for a phone interview tomorrow.  I have not written anything threaded in a long time.  I wrote a cute little multithreaded applet back in the 1990s for a class.  I also wrote a rather large Java application for regression testing a DSL access multiplexer through the SNMP interface in the late 1990s as well.  That had at least two threads in it, one to do the SNMP transactions, while the GUI was running.  But it has been so long, I needed to spend some time figuring out how this stuff works again!

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