+/EE360P

distributed systems

git clone https://git.julia.blue/+/EE360P

Joseph RyanFinish 3f5b5dc4

master
1.9 KiB57 linesraw
1import java.lang.Thread;
2import java.util.Random;
3
4public class testPriorityQueue implements Runnable {
5    final static int SIZE = 100;
6
7    final PriorityQueue priorityQueue;
8    final int index;
9
10    public testPriorityQueue(PriorityQueue priorityQueue, int i) {
11        this.priorityQueue = priorityQueue;
12        index = i;
13    }
14
15    public void run() {
16        Random rng = new Random();
17
18        int pri = rng.nextInt(10);
19        String name =
20            "" + (char)(rng.nextInt(26) + 'A') + (char)(rng.nextInt(26) + 'A');
21
22        float choice = rng.nextFloat();
23        if (choice > .5) {
24            System.out.println("adding " + name + " with priority: " + pri +
25                               " (" + index + ")");
26            System.out.println("completed add '" + name + "' at index " +
27                               priorityQueue.add("" + name, pri) + " (" +
28                               index + ")");
29        } else if (choice > .3) {
30            System.out.println("searching for " + name + " (" + index + ")");
31            System.out.println("search results for " + name + ": " +
32                               priorityQueue.search(name) + " (" + index + ")");
33        } else {
34            System.out.println("pop... (" + index + ")");
35            System.out.println("popped: " + priorityQueue.getFirst() + " (" +
36                               index + ")");
37        }
38    }
39
40    public static void main(String[] args) {
41        Thread[] t = new Thread[SIZE];
42
43        PriorityQueue priorityQueue = new PriorityQueue(SIZE * 3 / 4);
44
45        for (int i = 0; i < SIZE; ++i) {
46            t[i] = new Thread(new testPriorityQueue(priorityQueue, i));
47            t[i].setName("test thread " + i);
48        }
49
50        for (int i = 0; i < SIZE; ++i) { t[i].start(); }
51
52        try {
53            for (int j = 0; j < SIZE; j++) { t[j].join(); }
54        } catch (Exception e) { System.out.println(e); }
55        System.out.println(priorityQueue);
56    }
57}