目录
package xyz.jangle.thread.test.n2_3.synccondition;
/**
*
* 使用两个线程模拟生产者和消费者执行任务。
* 1、生产者将队列填充满之后会通知消费者进行消费。(应该是等待消费者进行消费)
* 2、消费者将队列消费为空之后,会通知生产者生产。(应该是等待生产者进行生产2024年1月3日10:10:16)
*
* 此处共同使用了EventStorage的对象锁,也就是synchronized方法锁,使用notify()方法进行通知。
* @author jangle
* @email jangle@jangle.xyz
* @time 2020年7月16日 下午10:19:19
*
*/
public class M {
public static void main(String[] args) {
EventStorage e = new EventStorage();
Producer p = new Producer(e); // 生产者线程
Consumer c = new Consumer(e); // 消费者线程
new Thread(p).start();
new Thread(c).start();
}
}
package xyz.jangle.thread.test.n2_3.synccondition;
/**
* @author jangle
* @email jangle@jangle.xyz
* @time 2020年7月16日 下午10:16:21
*
*/
public class Producer implements Runnable {
private EventStorage storage;
public Producer(EventStorage storage) {
super();
this.storage = storage;
}
@Override
public void run() {
for(int i=0;i<100;i++) {
storage.set();
}
}
}
package xyz.jangle.thread.test.n2_3.synccondition;
/**
* @author jangle
* @email jangle@jangle.xyz
* @time 2020年7月16日 下午10:18:00
*
*/
public class Consumer implements Runnable {
private EventStorage storage;
public Consumer(EventStorage storage) {
super();
this.storage = storage;
}
@Override
public void run() {
for (int i = 0; i < 100; i++)
storage.get();
}
}
此处共同使用了EventStorage的对象锁,也就是synchronized方法锁,使用notify()方法进行通知。
package xyz.jangle.thread.test.n2_3.synccondition;
import java.util.Date;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.TimeUnit;
/**
* @author jangle
* @email jangle@jangle.xyz
* @time 2020年7月16日 下午10:04:56
*
*/
public class EventStorage {
private int maxSize;
private Queue<Date> storage;
public EventStorage() {
maxSize = 10;
storage = new LinkedList<Date>();
}
public synchronized void set() {
while(storage.size() == maxSize) {
try {
System.out.println("生产满");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
storage.offer(new Date());
System.out.printf("Set: %d",storage.size());
notify();
}
public synchronized void get() {
while(storage.size() == 0) {
try {
System.out.println("消费完");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
String string = storage.poll().toString();
System.out.printf("Get: %d:%s \n",storage.size(),string);
notify();
}
}
Set: 1Set: 2Set: 3Set: 4Set: 5Set: 6Set: 7Set: 8Set: 9Set: 10生产满
Get: 9:Wed Jan 03 14:37:43 CST 2024
Get: 8:Wed Jan 03 14:37:43 CST 2024
Get: 7:Wed Jan 03 14:37:43 CST 2024
Get: 6:Wed Jan 03 14:37:43 CST 2024
Get: 5:Wed Jan 03 14:37:43 CST 2024
Get: 4:Wed Jan 03 14:37:43 CST 2024
Get: 3:Wed Jan 03 14:37:43 CST 2024
Get: 2:Wed Jan 03 14:37:43 CST 2024
Get: 1:Wed Jan 03 14:37:43 CST 2024
Get: 0:Wed Jan 03 14:37:43 CST 2024
消费完
Set: 1Set: 2Set: 3Set: 4Set: 5Set: 6Set: 7Set: 8Set: 9Set: 10生产满
Get: 9:Wed Jan 03 14:37:43 CST 2024
Get: 8:Wed Jan 03 14:37:43 CST 2024
Get: 7:Wed Jan 03 14:37:43 CST 2024
Get: 6:Wed Jan 03 14:37:43 CST 2024
Get: 5:Wed Jan 03 14:37:43 CST 2024
Get: 4:Wed Jan 03 14:37:43 CST 2024
Get: 3:Wed Jan 03 14:37:43 CST 2024
Get: 2:Wed Jan 03 14:37:43 CST 2024
Get: 1:Wed Jan 03 14:37:43 CST 2024
Get: 0:Wed Jan 03 14:37:43 CST 2024
消费完
Set: 1Set: 2Set: 3Set: 4Set: 5Set: 6Set: 7Set: 8Set: 9Set: 10生产满
Get: 9:Wed Jan 03 14:37:43 CST 2024
Get: 8:Wed Jan 03 14:37:43 CST 2024
Get: 7:Wed Jan 03 14:37:43 CST 2024
Get: 6:Wed Jan 03 14:37:43 CST 2024
Get: 5:Wed Jan 03 14:37:43 CST 2024
Get: 4:Wed Jan 03 14:37:43 CST 2024
Get: 3:Wed Jan 03 14:37:43 CST 2024
Get: 2:Wed Jan 03 14:37:43 CST 2024
Get: 1:Wed Jan 03 14:37:43 CST 2024
Get: 0:Wed Jan 03 14:37:43 CST 2024
消费完
...
...
...