package abit;public class classicThread { public static void main(String[] args){ Container c=new Container(); Producer p=new Producer(c); Consumer c2=new Consumer(c); new Thread(p).start(); new Thread(c2).start(); }}class Product{int id;Product(int id){this.id=id;}public String toString(){return "产品"+id;}}class Container{int index=0;Product[] product=new Product[6];//规定这个容器大小是6个public synchronized void add(Product p){while(index==product.length){try{this.wait();//Container c所在的线程等待}catch(Exception e){}}//this.notify();product[index]=p;index++;}public synchronized Product remove(){while(index==0){try{this.wait();}catch(Exception e){}}this.notify();index--;return product[index];}}class Producer implements Runnable{Container c=null;Producer(Container c){this.c=c;}public void run(){for(int i=0;i<20;i++){Product p=new Product(i);c.add(p);System.out.println("生产了"+p);// try{// Thread.sleep(1000);// }catch(Exception e){}}}}class Consumer implements Runnable{Container c=null;Consumer(Container c){this.c=c;}public void run(){for(int i=0;i<20;i++){Product p=c.remove();System.out.println("----消费了"+p);try{Thread.sleep(2000);}catch(Exception e){}}}}容器只能装6个,在run中生产20个,然后不断取走,保证不会超过容器的大小(让线程睡眠只是让效果更佳明显而已)能够放入的临界条件是while(index==product.length),能够取走的临界条件是while(index==0)执行start()的时候其实就是在run()中调用了class Container中的方法class Container 中的方法都是只能是一个线程进入的,wait()的时候会释放锁。public final void notify()唤醒在此对象监视器上等待的单个线程。如果所有线程都在此对象上等待,则会选择唤醒其中一个线程。选择是任意性的,并在对实现做出决定时发生。线程通过调用其中一个 wait 方法,在对象的监视器上等待。 public final void wait() throws InterruptedException在其他线程调用此对象的 notify() 方法或 notifyAll() 方法前,导致当前线程等待。换句话说,此方法的行为就好像它仅执行 wait(0) 调用一样。 这个例子中,这个对象其实一直是Container c, add()方法睡着时,另外一条线程即消费者线程调用了this.notify(),是add()里的现在不再wati,而继续执行。
线程同步 生产者消费者小例子
最新推荐文章于 2024-01-27 17:24:18 发布