public class ProducerConsumer { public static void main(String[] args) { SyncStack ss = new SyncStack(); Producer p = new Producer(ss); Consumer c = new Consumer(ss); new Thread(p).start(); new Thread(c).start(); } } class WoTou { int id; WoTou(int id) { this.id = id; } public String toString() { return "WoTou : " + id; } } class SyncStack { int index = 0; WoTou[] arrWT = new WoTou[6]; public synchronized void push(WoTou wt) { while (index == arrWT.length) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.notifyAll(); arrWT[index] = wt; System.out.println("set: " + arrWT[index]); index++; } public synchronized WoTou pop() { while (index == 0) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.notifyAll(); index--; System.out.println("get :" + arrWT[index]); return arrWT[index]; } } class Producer implements Runnable { SyncStack ss = null; Producer(SyncStack ss) { this.ss = ss; } public void run() { for (int i = 0; i < 20; i++) { WoTou wt = new WoTou(i); ss.push(wt); try { Thread.sleep((int) (Math.random() * 200)); } catch (InterruptedException e) { e.printStackTrace(); } } } } class Consumer implements Runnable { SyncStack ss = null; Consumer(SyncStack ss) { this.ss = ss; } public void run() { for (int i = 0; i < 20; i++) { WoTou wt = ss.pop(); try { Thread.sleep((int) (Math.random() * 1000)); } catch (InterruptedException e) { e.printStackTrace(); } } } }
下面是自己的demo:
import org.apache.log4j.Logger; public class Consumer implements Runnable{ private Container container; Logger log = Logger.getLogger(Consumer.class); public Consumer(Container container){ this.container = container; } @Override public void run() { for(int i=0;i<10;i++){ try { Thread.sleep((int) (Math.random() * 1000)); container.get(); //log.info("消费了:"+container.get()); }catch (Exception e) { e.printStackTrace(); } } } }
import java.util.ArrayList; import java.util.List; import org.apache.log4j.Logger; public class Container { private List<Food> container; Logger log = Logger.getLogger(Container.class); public Container(List<Food> container){ this.container = container; } public synchronized Food get() throws Exception{ if(container == null){ container = new ArrayList<Food>(); } while(container.isEmpty()){ this.wait(); } Food food = container.remove(container.size()-1); log.info("get food:"+food+",container:"+container); this.notifyAll(); return food; } public synchronized void set(Food food) throws Exception{ if(container == null){ container = new ArrayList<Food>(); } while(container.size() >= 10){ this.wait(); } log.info("set food: "+food+",container:"+container); container.add(food); this.notifyAll(); } }
public class Food { private int id; public Food(int id){ this.id = id; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public String toString() { return "Food [id=" + id + "]"; } }
import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ProducerConsumerTest { public static void main(String[] args) { List<Food> list = new ArrayList<Food>(); Container container = new Container(list); Producer p = new Producer(container); Consumer c = new Consumer(container); //new Thread(p).start(); //new Thread(p2).start(); //new Thread(c).start(); ExecutorService pool = Executors.newFixedThreadPool(2); pool.execute(p); pool.execute(c); } }
相关推荐
5. **线程通信**:wait()、notify()、notifyAll()方法的使用,以及在生产者消费者模型、哲学家就餐问题等经典示例中的应用。 6. **线程池**:ExecutorService、ThreadPoolExecutor和Future接口的理解,线程池的配置...
9. **线程通信**:理解wait()、notify()和notifyAll()方法,以及在生产者消费者模型中的应用。 10. **原子操作与CAS**:学习Atomic类和CompareAndSwap(CAS)算法,以及它们在无锁编程中的应用。 通过学习这份预习...
6. **并发编程模式**:学习如何应用生产者-消费者模式、读者-写者模式等经典并发设计模式,以解决多线程场景下的常见问题。 7. **线程通信**:通过`wait()`、`notify()`和`notifyAll()`方法实现线程间的通信,理解...
3. **spring-jms**:此模块为Java消息服务(JMS)提供支持,包括生产者、消费者以及与消息驱动bean的集成。如果你的项目中需要使用消息队列来解耦系统组件或实现异步处理,这个模块将大有裨益。 4. **spring-...
6. **并发模式**:书中可能会介绍一些经典的并发编程模式,如生产者消费者模型、读写者模型、工作窃取等,这些都是解决并发问题的有效手段。 7. **并发异常处理**:多线程环境中,异常处理尤为重要。学习如何正确地...
4. 参赛选手:选手来自全校12个学院,背景多样,如退伍大学生士兵、国家励志奖学金获得者和青马工程学员。 5. 演讲题目:演讲题目反映了新时代青年的责任和对祖国发展的思考,如《新时代青年应砥砺前行》、《奋楫者...
该公司 认为: "数据已经渗透到当今每一个行业和业务职能领域, 成为 重要的生产因素, 人们对于海量数据的挖掘与运用, 预示着新 一波生产率增长和消费者盈余浪潮的到来" 。 其实, "大数据" 早 已存在, 但由于...