-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadedApp.java
More file actions
91 lines (70 loc) · 4.22 KB
/
ThreadedApp.java
File metadata and controls
91 lines (70 loc) · 4.22 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class ThreadedApp {
public static void main(String[] args) {
Instant start = Instant.now();
int numberOfObjects = 12; // Number of experiments to execute
int numberOfThreads = Runtime.getRuntime().availableProcessors();
System.out.println("Will be executing "+ numberOfObjects + " experiments in " +numberOfThreads+ " threads." );
int minValue = Integer.MAX_VALUE;
// int[][] rectanglesDimensions = {
// {70, 20}, {100, 80}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40},
// {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {70, 20}, {100, 80}, {20, 40},
// {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {50, 40}, {20, 30}, {20, 20},
// {20, 40}, {100, 60}, {20, 40}, {50, 40}, {20, 30}, {20, 20}, {20, 80}, {100, 60}, {20, 40}, {20, 40},
// {10, 40}, {20, 10}, {15, 15}, {20, 20}, {15, 15}, {15, 15}, {15, 15}, {15, 15}, {15, 15}, {15, 15},
// {15, 15}, {15, 15}, {15, 15}, {15, 15}, {20, 20}, {20, 20}, {20, 20}, {20, 20}, {20, 20}, {20, 20},
// {70, 20}, {100, 80}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40},
// {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {70, 20}, {100, 80}, {20, 40},
// {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {50, 40}, {20, 30}, {20, 20},
// {20, 40}, {100, 60}, {20, 40}, {50, 40}, {20, 30}, {20, 20}, {20, 80}, {100, 60}, {20, 40}, {20, 40},
// {10, 40}, {20, 10}, {15, 15}, {20, 20}, {15, 15}, {15, 15}, {15, 15}, {15, 15}, {15, 15}, {15, 15},
// {15, 15}, {15, 15}, {15, 15}, {15, 15}, {20, 20}, {20, 20}, {20, 20}, {20, 20}, {20, 20}, {20, 20},
// {20, 20}, {20, 20}, {20, 20}, {20, 20}
// };
int[][] rectanglesDimensions = {
{70, 20}, {100, 80}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40},
{20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {70, 20}, {100, 80}, {20, 40},
{20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {20, 40}, {50, 40}, {20, 30}, {20, 20},
{20, 40}, {100, 60}, {20, 40}, {50, 40}, {20, 30}, {20, 20}, {20, 80}, {100, 60}, {20, 40}, {20, 40},
{10, 40}, {20, 10}, {15, 15}, {20, 20}, {15, 15}, {15, 15}, {15, 15}, {15, 15}, {15, 15}, {15, 15},
{15, 15}, {15, 15}, {15, 15}, {15, 15}, {20, 20}, {20, 20}, {20, 20}, {20, 20}, {20, 20}, {20, 20},
{20, 20}, {20, 20}, {20, 20}, {20, 20}
};
// Fixed thread pool for platform threads
ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads);
List<Future<Experiment>> results = new ArrayList<>();
try {
// Create experiments
for (int i = 0; i < numberOfObjects; i++) {
Experiment task = new Experiment( i,rectanglesDimensions);
results.add( executor.submit(task::performTask));
}
// Collect results and calculate the minimum value
Experiment winner=new Experiment(999999999,rectanglesDimensions);
for (Future<Experiment> future : results) {
Experiment result = future.get(); // Waits for the task to complete
minValue = Math.min(minValue, result.getScore());
if(result.getScore()==minValue)
{
winner=result;
}
}
winner.show(500,10);
} catch (Exception e) {
e.printStackTrace();
} finally {
executor.shutdown();
}
System.out.println("All experiments completed. Min val="+minValue);
Instant finish = Instant.now();
long timeElapsed = Duration.between(start, finish).toSeconds();
System.out.println("Execution time (s): "+timeElapsed);
System.exit(0);
}
}