Listing A: Stopping a thread with Thread.interrupt()
class Example1 extends Thread {
  public static void main( String args[] ) throws Exception {
    Example1 thread = new Example1();
   System.out.println( "Starting thread..." );
   thread.start();
   Thread.sleep( 3000 );
   System.out.println( "Interrupting thread..." );
   thread.interrupt();
   Thread.sleep( 3000 );
   System.out.println( "Stopping application..." );
   System.exit( 0 );
  }
 
  public void run() {
    while ( true ) {
     System.out.println( "Thread is running..." );
      long time = System.currentTimeMillis();
      while ( System.currentTimeMillis()-time < 1000 ) {
      }
    }
  }
}

Do you need help with Java, C, or C++? Gain advice from Builder AU forums

Related links

Comments

1

Shahrine - 28/11/06

there is no complete program for this?

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

1

Shahrine - 28/11/06

there is no complete program for this? ... more

Log in


Sign up | Forgot your password?

  • Staff Microsoft shows off IE9 preview

    This week, highlights from Microsoft's MIX10 conference and more in the Roundup. Read more »

    -- posted by Staff

  • Chris Duckett IE9's H.264 vote killed Ogg

    In a split decision by the judges, the winner of the W3C/WHATWG video codec consensus is H.264, taking home the future of video playback on the internet while loser Ogg goes home with nothing but thoughts of what might have been. Read more »

    -- posted by Chris Duckett

  • Staff Google launches Apps Marketplace

    Google launches and app store, while Mozilla plans to re-write its open-source license. More of this week's news in the Roundup. Read more »

    -- posted by Staff

What's on?

  • Optus Deal

    Broadband + home phone + PlayStation®3 in a single package price!