线程模拟生产 消费 模式

package test;

import java.util.LinkedList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class QueueTest {


public static void main(String[] args) {


final Object lock = new Object();
final LinkedList<String> queue = new LinkedList<>();


int maxSize = 10;


// Thread t2 = new Thread(new cusomer(queue, lock));
// t2.start();
//
// for (int i = 0; i < 20; i++) {
//
// Thread t1 = new Thread(new provider(queue, maxSize, lock, i + ""));
// t1.start();
//
// }

//======================
// final MyQueue testQueue = new MyQueue();
//
// new Thread(new Runnable() {
//
// @Override
// public void run() {
//
// for (int i = 0; i < 20; i++) {
// testQueue.add(i+"");
// }
// }
//
// }).start();
//
// new Thread(new Runnable() {
//
// @Override
// public void run() {
//
// for (int i = 0; i < 20; i++) {
// testQueue.get();
// }
// }
//
// }).start();

//=====================

ExecutorService service = Executors.newCachedThreadPool();
//ExecutorService service = Executors.newFixedThreadPool(21);
for (int i = 0; i < 20; i++) {
service.submit(new provider(queue, maxSize, lock, i + ""));
}

for (int i = 0; i < 20; i++) {
service.submit(new cusomer(queue, lock));
}

service.shutdown();



}


}


class provider implements Runnable {


private LinkedList<String> queue;


private int maxSize;


private String item;


private Object lock;


public provider(LinkedList<String> queue, int maxSize, Object lock, String item) {
this.queue = queue;
this.maxSize = maxSize;
this.lock = lock;
this.item = item;
}


@Override
public void run() {


synchronized (lock) {


try {


while (queue.size() >= maxSize) {
lock.wait();
}


queue.add(item);
System.out.println("加入元素 : " + item);
lock.notify();

} catch (Exception e) {
e.printStackTrace();
}


}


}


}


class cusomer implements Runnable {


private LinkedList<String> queue;


private Object lock;


public cusomer(LinkedList<String> queue, Object lock) {
this.queue = queue;
this.lock = lock;
}


@Override
public void run() {


synchronized (lock) {


try {

while(true) {

while (queue.isEmpty()) {
lock.wait();
}

System.out.println("取出元素 : " + queue.removeFirst());
lock.notify();

}


} catch (Exception e) {
e.printStackTrace();
}


}


}


}




class MyQueue {

Object lock = new Object();

private LinkedList<String> queue = new LinkedList<>();

int maxSize =10;

public void add(String item) {

synchronized (lock) {

try {

while(queue.size()>=maxSize) {
lock.wait();
}

queue.add(item);
System.out.println("加入元素 : " + item);
lock.notify();

} catch (Exception e) {
e.printStackTrace();
}

}

}

public void get() {

synchronized (lock) {

try {

while(queue.isEmpty()) {
lock.wait();
}

System.out.println("取出元素 : " + queue.removeFirst());
lock.notify();

} catch (Exception e) {
e.printStackTrace();
}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值