-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoarseTest.java
67 lines (63 loc) · 3.07 KB
/
CoarseTest.java
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
import java.util.Random;
import java.util.concurrent.atomic.*;
import java.util.*;
import java.lang.*;
import java.util.concurrent.*;
//javac -d . *.java
//java Program
public class CoarseTest{
static Coarse_Priority_Queue<Integer> concurrentQueue = new Coarse_Priority_Queue<Integer>();
public static void main(String[] args){
List<String> L = MetaTesting.parseInput("metadata.txt");
if(L==null || L.size() < 4){
System.out.println("Error parsing input");
return;
}
final int minInt = Integer.parseInt(L.get(0));
final int maxInt = Integer.parseInt(L.get(1));
final int threadCount = Integer.parseInt(L.get(2));
final AtomicInteger totalSteps = new AtomicInteger(Integer.parseInt(L.get(3)));
final int percentageInsert = Integer.parseInt(L.get(4));
final AtomicInteger step = new AtomicInteger(0);
Thread[] threads = new Thread[threadCount];
Random rand = new Random();
long instanceStart = System.currentTimeMillis();
for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread(new Runnable() {
private ThreadLocal<Integer> myStep = new ThreadLocal<Integer>();
public void run() {//START ADDING
while (step.incrementAndGet() <= totalSteps.get()){
myStep.set(step.get());
int number = rand.nextInt(100);
if((number%100)+1 < percentageInsert){
int toInsert = rand.nextInt(maxInt-minInt+1)+minInt;
System.out.println("Step " + myStep.get()+": Thread " + Thread.currentThread().getId()+ " - trying to insert..." + toInsert);
if(concurrentQueue.Add(toInsert))
System.out.println("Finished Step " + myStep.get() + ": Successfully inserted: " + toInsert);
else
System.out.println("Finished Step " + myStep.get() + ": Unable to insert " + toInsert);
}
else{
System.out.println("Step " + myStep.get()+": Thread " + Thread.currentThread().getId()+ " - trying to extract min...");
Integer min = concurrentQueue.Dequeue();
if(min!=null)
System.out.println("Finished Step " + myStep.get() + ": Extracted min... " + min);
else
System.out.println("Finished Step " + myStep.get() +" Unable to extract min");
}
}
}
});
threads[i].start();
}
for (Thread thread : threads) {
try{
thread.join();
}catch(InterruptedException e){System.out.println("Interrupted Exception");}
catch(NullPointerException e){System.out.println("Null Pointer exception");}
}
long instanceFinish = System.currentTimeMillis();
System.out.println("Time taken:" + (instanceFinish - instanceStart) + " milliseconds.");
concurrentQueue.Check();
}
}