forked from patniemeyer/learningjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNamedConsumer.java
More file actions
32 lines (27 loc) · 848 Bytes
/
Copy pathNamedConsumer.java
File metadata and controls
32 lines (27 loc) · 848 Bytes
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
//file: NamedConsumer.java
public class NamedConsumer implements Runnable
{
Producer producer;
String name;
NamedConsumer(String name, Producer producer) {
this.producer = producer;
this.name = name;
}
public void run() {
while ( true ) {
String message = producer.getMessage();
System.out.println(name + " got message: " + message);
try {
Thread.sleep( 2000 );
} catch ( InterruptedException e ) { }
}
}
public static void main(String args[]) {
Producer producer = new Producer();
new Thread( producer ).start();
NamedConsumer consumer = new NamedConsumer( "One", producer );
new Thread( consumer ).start();
consumer = new NamedConsumer( "Two", producer );
new Thread( consumer ).start();
}
}