Thread sleep and interrupt : Thread Status « Threads « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Threads » Thread Status 




Thread sleep and interrupt
Thread sleep and interrupt
 
public class SleepInterrupt extends Object implements Runnable {
  public void run() {
    try {
      System.out.println("in run() - sleep for 20 seconds");
      Thread.sleep(20000);
      System.out.println("in run() - woke up");
    catch (InterruptedException x) {
      System.out.println("in run() - interrupted while sleeping");
      return;
    }
    System.out.println("in run() - leaving normally");
  }

  public static void main(String[] args) {
    SleepInterrupt si = new SleepInterrupt();
    Thread t = new Thread(si);
    t.start();

    // Be sure that the new thread gets a chance to
    // run for a while.
    try {
      Thread.sleep(2000);
    catch (InterruptedException x) {
    }

    System.out.println("in main() - interrupting other thread");
    t.interrupt();
    System.out.println("in main() - leaving");
  }
}

           
         
  














Related examples in the same category
1.Is thread aliveIs thread alive
2.Thread sleepThread sleep
3.Another way to stop a threadAnother way to stop a thread
4.Another way to suspend and resumeAnother way to suspend and resume
5.Visual suspend and resumeVisual suspend and resume
6.Daemon ThreadDaemon Thread
7.Pausing the Current Thread: a thread can temporarily stop execution.
8.Pausing a Thread: set a variable that the thread checks occasionally, call Object.wait()
9.set Uncaught Exception Handler
10.Monitor a thread's status.
11.Pause the execution
12.Interrupt a thread.
13.Stopping a Thread: set a variable that the thread checks occasionally
14.Determining When a Thread Has Finished
15.Add a delay
16.Pause the execution of a thread using sleep()
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.