-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathTestVisualVMProgram.java
More file actions
56 lines (49 loc) · 2.27 KB
/
TestVisualVMProgram.java
File metadata and controls
56 lines (49 loc) · 2.27 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
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.Future;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;
// from https://urvanov.ru/2017/07/19/%D0%B4%D0%B8%D0%B0%D0%B3%D0%BD%D0%BE%D1%81%D1%82%D0%B8%D0%BA%D0%B0-java-%D0%BF%D1%80%D0%B8%D0%BB%D0%BE%D0%B6%D0%B5%D0%BD%D0%B8%D1%8F-%D1%81-jvisualvm/
//
public class TestVisualVMProgram {
private static final int THREAD_COUNT = 10;
private static final int TASKS_COUNT = 10;
private static final long WORK_MINUTES = 60L;
private static class MyClass{
private byte[] bytes = new byte[100_000];
}
private static final ConcurrentMap<Integer, MyClass> concurrentMap = new ConcurrentHashMap<>();
private static class MyTask implements Callable<Boolean> {
@Override
public Boolean call() throws Exception {
long startTime = System.currentTimeMillis();
// Каждый поток работает только WORK_MINUTES минут с момента старта.
while (System.currentTimeMillis() < startTime + 1000L * 60L* WORK_MINUTES) {
try {
Thread.sleep(500L);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
concurrentMap.put(
ThreadLocalRandom.current().nextInt(
Integer.MIN_VALUE, Integer.MAX_VALUE),
new MyClass());
}
return true;
}
}
public static void main(String[] args) throws InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(THREAD_COUNT);
Future<Boolean>[] futures = new Future[TASKS_COUNT];
System.out.println("Scheduling threads...");
for (int n = 0; n < TASKS_COUNT; n++) {
futures[n] = executorService.submit(new MyTask());
}
System.out.println("Awaiting termination...");
executorService.awaitTermination(WORK_MINUTES * 2L, TimeUnit.MINUTES);
System.out.println("Finished.");
}
}