Last active
August 29, 2015 14:07
-
-
Save easternHong/43fcd27fcae532688ed2 to your computer and use it in GitHub Desktop.
生产者,消费者模型,主要考虑线程同步(syncrhonized wait notify的使用
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//1.消费者,生产者都要实现Runnable接口 | |
//2.要有一个消费的对象,如:面包 | |
//3.模拟一个栈的操作方式,先进后出 | |
//4.线程同步,synchronized,notify,wait | |
public class AJava { | |
public static void main(String[] args) { | |
BasketStatck bs = new BasketStatck(); | |
Producer p = new Producer(bs); | |
Consumer c = new Consumer(bs); | |
new Thread(p).start(); | |
new Thread(c).start(); | |
} | |
} | |
//一个模拟堆的类,先进后出 | |
public class BasketStatck { | |
int index = 0; | |
// 1.定义栈空间。用面包来表示,空间大小为5 | |
Bread[] bbBread = new Bread[5]; | |
// 2.有出栈,入栈操作。出栈表示消费,入栈表示生产 | |
// 属于原子操作 | |
public synchronized void pushBread(Bread b) { | |
// 判断为满,这里要使用while | |
//即使是发生了exception 也要回来检查一次 | |
while (index == bbBread.length) { | |
try { | |
// 执行该方法的当前对象,也就是this. | |
this.wait(); | |
} catch (InterruptedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
this.notify(); | |
bbBread[index] = b; | |
index++; | |
System.out.println("生产了。。。" + b+" 还有: "+index); | |
} | |
public synchronized Bread popBread() { | |
// 判断为空,这里要使用while | |
while (index == 0) { | |
try { | |
// 线程等待,放开锁。。 | |
this.wait(); | |
} catch (InterruptedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
this.notify(); | |
index--; | |
System.out.println("消费了。。。" + bbBread[index]+" 还有: "+index); | |
return bbBread[index]; | |
} | |
} | |
public class Bread { | |
int id = 0; | |
Bread(int id) { | |
this.id = id; | |
} | |
//复写了 toString的方法 | |
public String toString(){ | |
return "bread :"+this.id; | |
} | |
} | |
public class Consumer implements Runnable { | |
BasketStatck bs = null; | |
// 构造函数,全局使用一个Basketstack | |
Consumer(BasketStatck s) { | |
this.bs = s; | |
} | |
@Override | |
public void run() { | |
// TODO Auto-generated method stub | |
for (int i = 0; i < 10; i++) { | |
Bread b = new Bread(i); | |
//调试信息不应该在这里打印,以免造成混乱,最好是在同步模块使用。 | |
bs.popBread(); | |
try { | |
Thread.sleep(2000L); | |
} catch (InterruptedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
//生产者,实现了Runnable 接口的run方法,放在子线程 | |
public class Producer implements Runnable { | |
BasketStatck bs = null; | |
Producer(BasketStatck s) { | |
this.bs = s; | |
} | |
@Override | |
public void run() { | |
// TODO Auto-generated method stub | |
for (int i = 0; i < 10; i++) { | |
Bread b = new Bread(i); | |
bs.pushBread(b); | |
//调试信息不应该在这里打印,以免造成混乱,最好是在同步模块使用。 | |
try { | |
Thread.sleep((int)Math.random()); | |
} catch (InterruptedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment