Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to resume a Thread after interrupting it's activity???

807601Jan 14 2008 — edited Jan 14 2008
Hi,
i'm new to threads and i'm not able to solve this out, so please help if you can. Special thanks to everyone.
Here's what i need:
i need to implement i timer which will periodically do an action and i need to interrupt and resume the activity (after a click for example - left-click start, right-click means stop). Here's my class:
public class MatrixTimer extends Thread
  {
    /** Default redraw-period in milliseconds. */
    public final static long DEFAULT_PERIOD = 25L;

    /** "Continue" flag for activity-period of the timer. */
    protected boolean cont;

    /** Actual timer period in milliseconds. */
    protected long period;

    /** Sets thread name for debugging identification. */
    public MatrixTimer ()
    {
      super( "Update-matrix timer" );
    }

    /**
     * Executive method of the timer thread.
     */
    @Override
    public void run ()
    {
      while ( cont && period > 0L )
      {
        try
        {
          sleep( period );
        }
        catch ( InterruptedException e )
        {
          System.out.println("InterruptedException ... "+isInterrupted());
        }
        synchronized ( this )//i don't know the exact meaning of synchronized statement and i'm not sure if it should be here and what should be it's  
                                      //body                
        {
        }
        
        // HERE COMES THE PERIODICAL ACTION
        updateMatrix();
      }
    }

    /**
     * Start timer.
     */
    public void startTimer ( long period )
    {
      if ( period < 0L )
        period = DEFAULT_PERIOD;

      this.period  = period;
      cont = true;
      try
      {
        setPriority( Thread.MIN_PRIORITY );
      }
      catch ( Exception e )
      {
        LogFile.exception( "Cannot set priority of timer thread! " + e.getMessage(), e );
      }
      try
      {
        start();
      }
      catch ( IllegalThreadStateException e )
      {
        LogFile.exception( "Cannot start timer thread! " + e.getMessage(), e );
      }
    }

    /**
     * Stop timer.
     */
    public void stopTimer ()
    {
      cont = false;
      interrupt();
    }
  }
so that's it. The problem is, that when i use startTimer for the first time, it works properly, then i invoke stopTimer with right-click, it sets cont flag to false, the activity of the timer stops and then i invoke interrupt() method (i found somewhere that this is how it should look like, first setting the flag which stops the while cycle in run() method and after that calling interrupt()). When i left-click again to call the startTimer() method, it throws an IllegalThreadStateException exception, cause it's not able to start the thread again. What's the bug? Do anyone know this? Thank you guys!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 11 2008
Added on Jan 14 2008
2 comments
3,160 views