-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProducer.java
More file actions
48 lines (38 loc) · 1.12 KB
/
Copy pathProducer.java
File metadata and controls
48 lines (38 loc) · 1.12 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
/**
* Producer.java
*
* This is the producer 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 Producer extends Thread
{
public Producer(BoundedBuffer b) {
buffer = b;
}
public void run()
{
//Date message;
int max = 50000;
int min = 8000;
int range = max - min + 1;
String name = this.getName();
while (true)
{
int sleeptime = (int) (BoundedBuffer.NAP_TIME_PRODUCER * Math.random()) +3;
System.out.println(name +" sleeping for " + sleeptime + " seconds");
try { sleep(sleeptime*1000); }
catch(InterruptedException e) {}
// produce an item & enter it into the buffer
int randNum = (int) (Math.random() * range) + min;
//message = new Date();
System.out.println(name +" produced " + randNum);
buffer.enter(randNum);
}
}
private BoundedBuffer buffer;
}