-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsumer.java
More file actions
60 lines (45 loc) · 1.31 KB
/
Copy pathConsumer.java
File metadata and controls
60 lines (45 loc) · 1.31 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
/**
* Consumer.java
*
* This is the consumer thread for the bounded buffer problem.
*
* @author Greg Gagne, Peter Galvin, Avi Silberschatz
* @version 1.0 - July 15, 1999
* Copyright 2000 by Greg Gagne, Peter Galvin, Avi Silberschatz
* Applied Operating Systems Concepts - John Wiley and Sons, Inc.
*/
import java.util.*;
public class Consumer extends Thread
{
public Consumer(BoundedBuffer b)
{
buffer = b;
}
public void run()
{
//Date message;
int bufferNum;
int sum;
int n;
String name = this.getName();
while (true)
{
int sleeptime = (int) (BoundedBuffer.NAP_TIME_CONSUMER * Math.random()) +4;
System.out.println(name +" sleeping for " + sleeptime + " seconds");
try { sleep(sleeptime*1000); }
catch(InterruptedException e) {}
// consume an item from the buffer
System.out.println(name +" wants to consume.");
//message = (Date)buffer.remove();
bufferNum = (int)buffer.remove();
sum = 0;
n = bufferNum;
while (n != 0) {
sum = sum + n % 10;
n = n/10;
}
System.out.println(name +" consumed "+bufferNum + " Sum of digits in " + name + ": " +sum);
}
}
private BoundedBuffer buffer;
}