java 生产者消费者问题-多线程与死锁
2017-04-20 13:38
525 查看
代码直接示例。
//想象这是馒头,每个馒头有自己的id
class Item{
private int id=0;
public Item(int id) {
this.id=id;
}
public int getID(){
return id+1;
}
}
//想象这是装馒头的框子
class Stack{
//最多装5个的数组
Item []arr=new Item[5];
int index=0; //记录装了几个
//添加与移除方法:都得上锁,不然乱了。关于这个问题点击打开链接,这里有一个例子找看看
public synchronized void add(Item it){
while(index==5){
try {
wait(); //如果满了就不能装了,线程wait
} catch (InterruptedException e) {
e.printStackTrace();
}
}
arr[index]=it;
index++;
notify(); //别忘了唤醒移除线程
}
public synchronized Item remove(){
while(index==0){
try {
this.wait(); 没了就不能去除了,wait
} catch (InterruptedException e) {
e.printStackTrace();
}
}
notify(); //唤醒添加线程。刚才不是wait了吗,然后进行一些操作:
index--;
Item it=arr[index];
arr[index]=null;
return it;
}
}
生产馒头的:生产者
class Produce implements Runnable{
Stack s=null;
public Produce(Stack s){
this.s=s;
}
//生产20个就算了吧,不然循环起来没完了
public void run(){
for(int i=0;i<20;i++){
s.add(new Item(i));
System.out.println("Pro: "+(i+1));
try {
Thread.sleep(400); 睡一会是为了更好的观察输出,你设成0的话 再看就看不出来运行过程了
} catch (InterruptedException e) {
e.printStackTrace();
}
4000
}
}
}
消费者
class Consume implements Runnable{
Stack s=null;
public Consume(Stack s){
this.s=s;
}
public void run(){
for(int i=0;i<20;i++){
System.out.println("Remove: "+ s.remove().getID());
try {
Thread.sleep(400);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class TestPandC{
public static void main(String[] args){
Stack s=new Stack();
new Thread(new Produce(s)).start();
new Thread(new Consume(s)).start();
}
}
运行一下看看吧
//想象这是馒头,每个馒头有自己的id
class Item{
private int id=0;
public Item(int id) {
this.id=id;
}
public int getID(){
return id+1;
}
}
//想象这是装馒头的框子
class Stack{
//最多装5个的数组
Item []arr=new Item[5];
int index=0; //记录装了几个
//添加与移除方法:都得上锁,不然乱了。关于这个问题点击打开链接,这里有一个例子找看看
public synchronized void add(Item it){
while(index==5){
try {
wait(); //如果满了就不能装了,线程wait
} catch (InterruptedException e) {
e.printStackTrace();
}
}
arr[index]=it;
index++;
notify(); //别忘了唤醒移除线程
}
public synchronized Item remove(){
while(index==0){
try {
this.wait(); 没了就不能去除了,wait
} catch (InterruptedException e) {
e.printStackTrace();
}
}
notify(); //唤醒添加线程。刚才不是wait了吗,然后进行一些操作:
index--;
Item it=arr[index];
arr[index]=null;
return it;
}
}
生产馒头的:生产者
class Produce implements Runnable{
Stack s=null;
public Produce(Stack s){
this.s=s;
}
//生产20个就算了吧,不然循环起来没完了
public void run(){
for(int i=0;i<20;i++){
s.add(new Item(i));
System.out.println("Pro: "+(i+1));
try {
Thread.sleep(400); 睡一会是为了更好的观察输出,你设成0的话 再看就看不出来运行过程了
} catch (InterruptedException e) {
e.printStackTrace();
}
4000
}
}
}
消费者
class Consume implements Runnable{
Stack s=null;
public Consume(Stack s){
this.s=s;
}
public void run(){
for(int i=0;i<20;i++){
System.out.println("Remove: "+ s.remove().getID());
try {
Thread.sleep(400);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class TestPandC{
public static void main(String[] args){
Stack s=new Stack();
new Thread(new Produce(s)).start();
new Thread(new Consume(s)).start();
}
}
运行一下看看吧
相关文章推荐
- JAVA基础再回首(二十五)——Lock锁的使用、死锁问题、多线程生产者和消费者、线程池、匿名内部类使用多线程、定时器、面试题
- JAVA基础再回首(二十五)——Lock锁的使用、死锁问题、多线程生产者和消费者、线程池、匿名内部类使用多线程、定时器、面试题
- JAVA基础再回首(二十五)——Lock锁的使用、死锁问题、多线程生产者和消费者、线程池、匿名内部类使用多线程、定时器、面试题
- Java多线程之生产者消费者问题
- 用JAVA实现多线程(生产者与消费者问题)
- 生产者与消费者问题(java多线程实现)
- 由生产者/消费者问题看JAVA多线程
- java多线程总结六:经典生产者消费者问题实现
- 由生产者/消费者问题看JAVA多线程
- 由生产者/消费者问题看JAVA多线程
- Java 多线程 生产者与消费者问题测试代码
- java例程练习(多线程综合练习[生产者-消费者问题])
- java多线程 生产者消费者问题
- java例程练习(多线程综合练习[生产者-消费者问题])
- 由生产者/消费者问题看JAVA多线程
- java多线程 生产者 消费者 问题 。。。
- 由生产者/消费者问题看JAVA多线程
- 回顾生产者/消费者问题下产生的java多线程(二)
- 由生产者/消费者问题看JAVA多线程
- java 多线程经典例子——生产者与消费者的问题