Dienstag, 3. März 2009

CS4282/CS6282--Thread Objects

CS4282/CS6282 Internet and Distributed Systems Programming

Doug Lea : Concurrent Programming in Java
notes on page 9

Thread Objects:

new Thread(aRunnableObj ).start();

1. The most commonly used constructor in class Thread accepts a Runnable object as an argument, which arranges to invoke the Runnable's run method when the thread is started.

2. Whle any class can implement Runnable, it often turns out to be both convenient and helpful to define a Runnable as an anonymous inner class.

eg :

public Thread makeThread( Particle p) {

Runnable runloop = new Runnable() {
public void run() {
try {
while (true) {
p.move();
canvasPanel.repaint();
Thread.sleep(100);
} // while
} // try
catch( InterruptedExceprion ex)
{
return;
}
}
}; // anonymous inner class
return new Thread(runloop);
}





Keine Kommentare: