SE450: Why is Output Corrupted? [21/36] |
file:horstmann/ch09_queue1/ThreadTester.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package horstmann.ch09_queue1; /** This program runs two threads in parallel. */ public class ThreadTester { public static void main(String[] args) { BoundedQueue<String> queue = new BoundedQueue<String>(10); queue.setDebug(true); final int GREETING_COUNT = 100; Runnable run1 = new Producer("Hello, World!", queue, GREETING_COUNT); Runnable run2 = new Producer("Goodbye, World!", queue, GREETING_COUNT); Runnable run3 = new Consumer(queue, 2 * GREETING_COUNT); Thread thread1 = new Thread(run1); Thread thread2 = new Thread(run2); Thread thread3 = new Thread(run3); thread1.start(); thread2.start(); thread3.start(); } }
file:horstmann/ch09_queue1/Producer.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
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
package horstmann.ch09_queue1; /** An action that repeatedly inserts a greeting into a queue. */ public class Producer implements Runnable { /** Constructs the producer object. @param aGreeting the greating to insert into a queue @param aQueue the queue into which to insert greetings @param count the number of greetings to produce */ public Producer(String aGreeting, BoundedQueue<String> aQueue, int count) { greeting = aGreeting; queue = aQueue; greetingCount = count; } public void run() { try { int i = 1; while (i <= greetingCount) { if (!queue.isFull()) { queue.add(i + ": " + greeting); i++; } Thread.sleep((int) (Math.random() * DELAY)); } } catch (InterruptedException exception) { } } private String greeting; private BoundedQueue<String> queue; private int greetingCount; private static final int DELAY = 10; }
file:horstmann/ch09_queue1/Consumer.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
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
package horstmann.ch09_queue1; /** An action that repeatedly removes a greeting from a queue. */ public class Consumer implements Runnable { /** Constructs the consumer object. @param aQueue the queue from which to retrieve greetings @param count the number of greetings to consume */ public Consumer(BoundedQueue<String> aQueue, int count) { queue = aQueue; greetingCount = count; } public void run() { try { int i = 1; while (i <= greetingCount) { if (!queue.isEmpty()) { String greeting = queue.remove(); System.out.println(greeting); i++; } Thread.sleep((int)(Math.random() * DELAY)); } } catch (InterruptedException exception) { } } private BoundedQueue<String> queue; private int greetingCount; private static final int DELAY = 10; }
file:horstmann/ch09_queue1/BoundedQueue.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
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
package horstmann.ch09_queue1; import java.util.ArrayList; /** A first-in, first-out bounded collection of objects. */ public class BoundedQueue<E> { /** Constructs an empty queue. @param capacity the maximum capacity of the queue */ public BoundedQueue(int capacity) { elements = new ArrayList<E>(capacity); head = 0; tail = 0; size = 0; } /** Removes the object at the head. @return the object that has been removed from the queue <p><b>Precondition:</b> !isEmpty()</p> */ public E remove() { if (debug) System.out.print("removeFirst"); E r = elements.get(head); if (debug) System.out.print("."); head++; if (debug) System.out.print("."); size--; if (head == elements.size()) { if (debug) System.out.print("."); head = 0; } if (debug) System.out.println("head=" + head + ",tail=" + tail + ",size=" + size); return r; } /** Appends an object at the tail. @param newValue the object to be appended <p><b>Precondition:</b> !isFull();</p> */ public void add(E newValue) { if (debug) System.out.print("add"); elements.set(tail,newValue); if (debug) System.out.print("."); tail++; if (debug) System.out.print("."); size++; if (tail == elements.size()) { if (debug) System.out.print("."); tail = 0; } if (debug) System.out.println("head=" + head + ",tail=" + tail + ",size=" + size); } public boolean isFull() { return size == elements.size(); } public boolean isEmpty() { return size == 0; } public void setDebug(boolean newValue) { debug = newValue; } private ArrayList<E> elements; private int head; private int tail; private int size; private boolean debug; }