-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadTest.java
More file actions
62 lines (51 loc) · 1.59 KB
/
ThreadTest.java
File metadata and controls
62 lines (51 loc) · 1.59 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
package com.java.test;
import java.time.LocalTime;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class ThreadTest implements Runnable {
static LocalTime startTime, endTime;
static ThreadGroup tg = new ThreadGroup("ronTG");
@Override
public void run() {
for(int x = 0; x < 10; x ++) {
System.out.println("running thread: " + Thread.currentThread().getName() + " " + this.hashCode());
try {
testSync();
// tg.wait();
System.out.println(tg.getName());
Thread.sleep(2000);
Thread.currentThread().yield();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized static void testSync() {
System.out.println("called sync test...");
}
public static void main(String[] args) throws InterruptedException {
ThreadTest tt1 = new ThreadTest();
Thread t1 = new Thread(tg, tt1, "tt1");
Thread t2 = new Thread(tg, tt1, "tt2");
Thread t3 = new Thread(tg, tt1, "tt3");
t2.setPriority(Thread.MAX_PRIORITY);
t3.setPriority(Thread.MIN_PRIORITY);
t1.setPriority(Thread.MIN_PRIORITY);
startTime = LocalTime.now();
System.out.println("Start time: " + startTime);
// t1.start();
// t1.join();
// t2.start();
// t3.start();
// Executor r = Executors.newCachedThreadPool();
// r.execute(t1);
Executor rFixed = Executors.newFixedThreadPool(3);
rFixed.execute(t1);
rFixed.execute(t2);
rFixed.execute(t3);
endTime = LocalTime.now();
System.out.println("End time: " + endTime);
t3.join();
System.out.println("Time Diff: " + endTime.minusMinutes(startTime.getMinute()));
}
}