I'm just starting with threads, and I'm having a bit of trouble... I have a class called Timer, which basically draws a filled rectangle in a decremented for loop as shown:<br>
<br>
class Timer extends Thread {<br>
private Graphics g;<br>
<br>
public Timer (Graphics graphics) {<br>
g = graphics;<br>
}<br>
public void run () {<br>
for (int width = 250; width >= 0; width--) {<br>
if (width > 40) g.setColor(Color.green);<br>
else g.setColor(Color.red);<br>
g.fillRect(8, 36, width, 6);<br>
g.setColor(Color.black);<br>
g.fillRect((width + 8), 36, (250 - width), 6);<br>
try {<br>
Thread.sleep(80);<br>
}<br>
catch (InterruptedException e) {<br>
System.err.println("sleep exception in timer"<br>
}<br>
}<br>
}<br>
}<br>
<br>
in my main applet, I have "timer = new Timer(g); timer.start();" when a button is pressed.<br>
<br>
Here's my problem... something needs to be invoked when the timer "runs out", ie, when the for loop is done. It's rather easy to do something inside of the Timer class, but I would much rather be able to control it from inside the main applet class which is invoking the timer. Is there some kind of ThreadListener that is aware when a thread has run its course? How do you detect when a thread has finished?<br>
<br>
Much thanks... <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence."
<br>
class Timer extends Thread {<br>
private Graphics g;<br>
<br>
public Timer (Graphics graphics) {<br>
g = graphics;<br>
}<br>
public void run () {<br>
for (int width = 250; width >= 0; width--) {<br>
if (width > 40) g.setColor(Color.green);<br>
else g.setColor(Color.red);<br>
g.fillRect(8, 36, width, 6);<br>
g.setColor(Color.black);<br>
g.fillRect((width + 8), 36, (250 - width), 6);<br>
try {<br>
Thread.sleep(80);<br>
}<br>
catch (InterruptedException e) {<br>
System.err.println("sleep exception in timer"<br>
}<br>
}<br>
}<br>
}<br>
<br>
in my main applet, I have "timer = new Timer(g); timer.start();" when a button is pressed.<br>
<br>
Here's my problem... something needs to be invoked when the timer "runs out", ie, when the for loop is done. It's rather easy to do something inside of the Timer class, but I would much rather be able to control it from inside the main applet class which is invoking the timer. Is there some kind of ThreadListener that is aware when a thread has run its course? How do you detect when a thread has finished?<br>
<br>
Much thanks... <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence."