-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserThreadOrDemonThread.java
More file actions
52 lines (35 loc) · 1.01 KB
/
Copy pathUserThreadOrDemonThread.java
File metadata and controls
52 lines (35 loc) · 1.01 KB
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
package codingInterview.thread;
import java.util.Random;
public class UserThreadOrDemonThread {
public static void main(String str[]) throws Exception {
System.out.println("Entry");
new DemonThread().start();
Thread.sleep(1000);
System.out.println("Exit");
System.out.println("Exit ----------------------");
}
}
class DemonThread extends Thread {
DemonThread() {
setDaemon(true);//If main thread Exit Daemon thread Exit
//setDaemon(false);//If main thread Exit User Thread still continue
}
static int calcGCD(int A, int B) {
if (B == 0)
return A;
else
return calcGCD(B, A % B);
}
@Override
public void run() {
Random r = new Random();
for (int i = 0; i < 1000; i++) {
//setDaemon(true);//Internally call isAlive method to to
int A = r.nextInt();
int B = r.nextInt();
System.out.println(this.currentThread() + ":" + calcGCD(A, B));
}
System.out.println(this.currentThread() +"Exit");
}
//setDaemon(false);
}