| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 580 人关注过本帖
标题:线程同步问题
只看楼主 加入收藏
specilize
Rank: 4
等 级:业余侠客
帖 子:126
专家分:247
注 册:2011-2-20
结帖率:100%
收藏
已结贴  问题点数:0 回复次数:3 
线程同步问题
import java.util.*;
class Store{
    private int seq;
    private boolean available=false;
    public synchronized int get(){
        while(available==false){
            try{
                wait();
            }
            catch(InterruptedException e){            
            }
        }
        available=false;
        notify();
        return seq;
    }
    public synchronized void put(int value){
        while(available==true){
            try{
                wait();
            }
            catch(InterruptedException e){            
            }
        }
        seq=value;
        available=true;
        notify();
    }
}

class Producer extends Thread{
    private Store store;
    private  int num;
    public Producer(Store s,int num){
        store=s;
        this.num=num;
    }
    public void run(){
        for(int i=0;i<10;++i){
            store.put(i);
            System.out.println("Producer #"+this.num+"put:"+i);
            try{
                sleep((int)(Math.random()*1000));
               
            }
            catch(InterruptedException e){
               
            }  
        }
    }
}

class Consumer extends Thread{
    private Store store;
    private int num;
    public Consumer(Store s,int num){
        store=s;
        this.num=num;
    }
    public void run(){
        int value=0;
        for(int i=0;i<10;++i){
            value=store.get();
            System.out.println("Consumer #"+this.num+"get:"+value);
        }
    }
}

class MyPrCon {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
       Store s=new Store();
       Producer p=new Producer(s,2);
       Consumer c=new Consumer(s,2);
       p.start();
       c.start();
    }
}
输出结果是
Producer #2put:0
Consumer #2get:0
Consumer #2get:1
Producer #2put:1
Consumer #2get:2
Producer #2put:2
Consumer #2get:3
Producer #2put:3
Consumer #2get:4
Producer #2put:4
Producer #2put:5
Consumer #2get:5
Consumer #2get:6
Producer #2put:6
Consumer #2get:7
Producer #2put:7
Consumer #2get:8
Producer #2put:8
Consumer #2get:9
Producer #2put:9
为什么会这样呢,本来是要一个输出Producer,一个输出Consumer的,却出现输出两个Consumer的状况?
搜索更多相关主题的帖子: available private public return import 
2011-09-26 23:15
lucky563591
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:765
专家分:2103
注 册:2009-11-18
收藏
得分:20 
次线程执行顺序是随机的
2011-09-27 08:18
specilize
Rank: 4
等 级:业余侠客
帖 子:126
专家分:247
注 册:2011-2-20
收藏
得分:0 
回复 2楼 lucky563591
那不就相当于Store的get和put有同步和没同步是一样的,没同步时输出也是随机的啊,那怎样实现先输出一个Producer后输出一个Comsumer
2011-09-27 12:43
specilize
Rank: 4
等 级:业余侠客
帖 子:126
专家分:247
注 册:2011-2-20
收藏
得分:0 
回复 2楼 lucky563591
我明白你得意思了,应该这样写
import java.util.*;

class Store{
    private int seq;
    private boolean available=false;
    public synchronized int get(int num){
        while(available==false){
            try{
                wait();
            }
            catch(InterruptedException e){            
            }
        }
        System.out.println("Consumer#"+num+"get:"+seq);
        available=false;
        notify();
        return seq;
    }
   
    public synchronized void put(int value,int num){
        while(available==true){
            try{
                wait();
            }
            catch(InterruptedException e){            
            }
        }
        System.out.println("Procuder#"+num+"put:"+value);
        seq=value;
        available=true;
        notify();
    }
}

class Producer extends Thread{
    private Store store;
    private  int num;
    public Producer(Store s,int num){
        store=s;
        this.num=num;
    }
    public void run(){
        for(int i=0;i<10;++i){
            store.put(i,num);
        //    System.out.println("Producer #"+this.num+"put:"+i);
            try{
                sleep((int)(Math.random()*1000));
               
            }
            catch(InterruptedException e){
               
            }  
        }
    }
}

class Consumer extends Thread{
    private Store store;
    private int num;
    public Consumer(Store s,int num){
        store=s;
        this.num=num;
    }
    public void run(){
        int value=0;
        for(int i=0;i<10;++i){
            value=store.get(num);
        //    System.out.println("Consumer #"+this.num+"get:"+value);
        }
    }
}

public class MyPrCon {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
       Store s=new Store();
       Producer p=new Producer(s,2);
       Consumer c=new Consumer(s,2);
       p.start();
       c.start();
    }
}

2011-09-27 22:09
快速回复:线程同步问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.078352 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved