-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserThreadOrDemonExecutor.java
More file actions
63 lines (44 loc) · 1.29 KB
/
Copy pathUserThreadOrDemonExecutor.java
File metadata and controls
63 lines (44 loc) · 1.29 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
53
54
55
56
57
58
59
60
61
62
63
package codingInterview.thread;
import java.util.Random;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
public class UserThreadOrDemonExecutor {
public static void main(String str[]) throws Exception {
System.out.println("Entry");
ThreadFactory thf = new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
System.out.println("Called THread Factory");
Thread th = new Thread(r);
th.setDaemon(false);
return th;
}
};
Executor service = Executors.newFixedThreadPool(2, thf);
for (int i = 0; i < 2; i++)
service.execute(new DemonThreadEx());
System.out.println("Exit ----------------------");
}
}
class DemonThreadEx implements Runnable {
static int count = 1;
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("Runnable" + count + ":" + calcGCD(A, B));
}
System.out.println("Runnable" + count++ + "Exit");
}
// setDaemon(false);
}