Thursday, February 6, 2014

Java - Thread (sleep, join, isAlive, interrupt)

public class Test extends Thread {

public void run() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void main(String[] args) {
Test test = new Test();

test.start();
try {
int count = 0;

while (count < 5) {

test.join(1000);

count++;
System.out.format("%d second(s) waited\n", count);
}

if (test.isAlive()) {
test.interrupt();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.