传统方式
import java. util. LinkedList ;
import java. util. Queue ;
import java. util. concurrent. atomic. AtomicInteger ;
public class ProducerAndConsumer3 {
public static void main ( String [ ] args) {
Clerk3 clerk3 = new Clerk3 ( ) ;
for ( int i = 0 ; i < 5 ; i++ ) {
Prod3 p = new Prod3 ( clerk3) ;
Thread tp = new Thread ( p, "producer" + i) ;
tp. start ( ) ;
}
for ( int i = 0 ; i < 20 ; i++ ) {
Cons3 c = new Cons3 ( clerk3) ;
Thread tc = new Thread ( c, "consumer" + i) ;
tc. start ( ) ;
}
}
}
class Clerk3 {
private AtomicInteger ai = new AtomicInteger ( ) ;
private Queue < String > queue = new LinkedList < > ( ) ;
public synchronized void addProduce ( ) {
try {
Thread . sleep ( 100 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
if ( queue. size ( ) >= 10 ) {
try {
wait ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
} else {
String data = ai. incrementAndGet ( ) + "" ;
queue. add ( data) ;
System . out. println ( "生产者线程" + Thread . currentThread ( ) . getName ( ) + "生产了第" + data+ "商品" ) ;
notifyAll ( ) ;
}
}
public synchronized void getProduce ( ) {
try {
Thread . sleep ( 100 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
if ( queue. size ( ) <= 0 ) {
try {
wait ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
} else {
String data = queue. poll ( ) ;
System . out. println ( "消费者线程" + Thread . currentThread ( ) . getName ( ) + "消费了第" + data+ "商品" ) ;
notifyAll ( ) ;
}
}
}
class Prod3 implements Runnable {
Clerk3 clerk;
public Prod3 ( Clerk3 clerk) {
this . clerk = clerk;
}
@Override
public void run ( ) {
System . out. println ( "生产者开始生产产品" ) ;
while ( true ) {
try {
Thread . sleep ( ( int ) Math . random ( ) * 1000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
clerk. addProduce ( ) ;
}
}
}
class Cons3 implements Runnable {
Clerk3 clerk;
public Cons3 ( Clerk3 clerk) {
this . clerk = clerk;
}
@Override
public void run ( ) {
System . out. println ( "消费者开始消费产品" ) ;
while ( true ) {
try {
Thread . sleep ( ( int ) Math . random ( ) * 1000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
clerk. getProduce ( ) ;
}
}
}
阻塞队列
import java. util. concurrent. ArrayBlockingQueue ;
import java. util. concurrent. BlockingQueue ;
import java. util. concurrent. atomic. AtomicInteger ;
public class ProducerAndConsumerBlock3 {
public static void main ( String [ ] args) {
Waiter waiter = new Waiter ( new ArrayBlockingQueue ( 10 ) ) ;
for ( int i = 0 ; i < 5 ; i++ ) {
CoffeeMarker cm = new CoffeeMarker ( waiter) ;
new Thread ( cm, "生产线程" + i) . start ( ) ;
}
for ( int i = 0 ; i < 20 ; i++ ) {
Cons c = new Cons ( waiter) ;
new Thread ( c, "消费线程" + i) . start ( ) ;
}
}
}
class Waiter {
AtomicInteger ai = new AtomicInteger ( ) ;
BlockingQueue < String > bq = null ;
public Waiter ( BlockingQueue bq) {
this . bq = bq;
}
public void addCoffe ( ) {
String coffee = ai. incrementAndGet ( ) + "" ;
try {
System . out. println ( "咖啡师" + Thread . currentThread ( ) . getName ( ) + "生产一杯编号为" + coffee+ "的coffee" ) ;
bq. put ( coffee) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
public void removeCoffe ( ) {
try {
String c = bq. take ( ) ;
System . out. println ( "消费者" + Thread . currentThread ( ) . getName ( ) + "喝了一杯编号为" + c+ "的coffee" ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
class CoffeeMarker implements Runnable {
private Waiter waiter;
public CoffeeMarker ( Waiter waiter) {
System . out. println ( "来了一个咖啡师" ) ;
this . waiter = waiter;
}
@Override
public void run ( ) {
while ( true ) {
try {
Thread . sleep ( 100 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
waiter. addCoffe ( ) ;
}
}
}
class Cons implements Runnable {
private Waiter waiter;
public Cons ( Waiter waiter) {
System . out. println ( "来了一个顾客" ) ;
this . waiter = waiter;
}
@Override
public void run ( ) {
while ( true ) {
try {
Thread . sleep ( 100 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
waiter. removeCoffe ( ) ;
}
}
}