package core freak import java io IOException public class ThreadTest

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package core.freak;
import java.io.IOException;
public class ThreadTest {
public ThreadTest() {
}
public void start() {
System.out.println("Start daemon..");
t= new Thread(new Daemon(Thread.currentThread()), "Daemon");
t.start();
System.out.println("Main thread sleep..");
try {
Thread.sleep(5000);
if(t.isAlive()) {
System.out.println("Daemon is still running...\nKilling...");
t.interrupt();
}
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("Main thread interrupted!");
}
System.out.println("Status is " + status);
}
private class Daemon implements Runnable {
public Daemon(Thread arg) {
tt= arg;
}
public void run() {
String query = "C:\\Windows\\notepad.exe";
try {
p = Runtime.getRuntime().exec(query);
if (p.waitFor() == 0) {
status = true;
} else {
status = false;
}
tt.interrupt();
} catch (InterruptedException e) {
p.destroy();
e.printStackTrace();
status = false;
} catch (IOException e) {
e.printStackTrace();
status = false;
}
}
private Thread tt;
private Process p;
}
public static void main(String[] args) {
ThreadTest t = new ThreadTest();
t.start();
}
private Thread t;
private boolean status = false;
}