Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Threads as events

Status
Not open for further replies.

carpeliam

Programmer
Mar 17, 2000
990
US
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 &gt;= 0; width--) {<br>
            if (width &gt; 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(&quot;sleep exception in timer&quot;);<br>
            }<br>
        }<br>
    }<br>
}<br>
<br>
in my main applet, I have &quot;timer = new Timer(g); timer.start();&quot; when a button is pressed.<br>
<br>
Here's my problem... something needs to be invoked when the timer &quot;runs out&quot;, 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."
 
Hi!<br>
<br>
isAlive();<br>
<br>
But when the run() is finished, the thread will be destroyed, and can not be accessed.<br>
If you can only invoke something when the run is finished, make and set a &quot;finished&quot; variable, or simply invoke the desired method of the applet.<br>
<br>
Good luck. Bye, Otto.<br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top