| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 441 人关注过本帖
标题:线程问题,请帮忙看下,谢谢!
取消只看楼主 加入收藏
笔墨痕干
Rank: 1
等 级:新手上路
威 望:1
帖 子:56
专家分:0
注 册:2014-3-24
结帖率:84.21%
收藏
已结贴  问题点数:18 回复次数:1 
线程问题,请帮忙看下,谢谢!
程序代码:
package 线程;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Reasource2
{
    private String name;
    private int count=1;
    private boolean flage=false;
    private Lock lock=new ReentrantLock();
    private Condition condition_pro  = lock.newCondition();
    private Condition condition_com  = lock.newCondition();
    public  void set(String name) throws InterruptedException//生产
    {
        lock.lock();
        try
        {
        while(flage==true)
            condition_pro.wait();
        this.name=name+"......."+count++;
//        Thread.currentThread().setName("生产者");
        System.out.println(Thread.currentThread().getName()+".....生产者。。。。。"+"....."+this.name);
        flage=true;
        condition_com.signal();
        }
        finally
        {
        lock.unlock();
        }
    }
    public  void out() throws InterruptedException//消费
    {
        lock.lock();
        try
        {
        while(flage==false)
            condition_com.wait();
//        Thread.currentThread().setName("消费者");
        System.out.println(Thread.currentThread().getName()+"...消费者"+"....."+this.name);
        flage=false;
        condition_pro.signal();
        }
        finally
        {
           
            lock.unlock();
        }
       
    }
}
class Producer2 implements Runnable//生产者
{
    private Reasource2 r;
    private int x=0;
    Producer2(Reasource2 r)
    {
        this.r=r;
    }
    public void run()
    {
        while(true)
        {
            if(x==0)
                try {
                    r.set("这个");
                } catch (InterruptedException e) {
               
                }
            else
                try {
                    r.set("那个");
                } catch (InterruptedException e) {
                   
                }
            x=(x+1)%2;
        }
    }
}
class Consumer2 implements Runnable//消费者
{
    Reasource2 r;
    public  Consumer2(Reasource2 r)
    {
        this.r=r;
    }
    public void run()
    {
        while(true)
            try {
                r.out();
            } catch (InterruptedException e) {
               
            }
    }
}
public class ProducerConsumerDemo2 {

    public static void main(String[] args)
    {
        Reasource2 r=new Reasource2();
       
        Producer2 pro=new Producer2(r);
        Consumer2 con=new Consumer2(r);
       
        Thread t1=new Thread(pro);
       
        Thread t3=new Thread(pro);
        Thread t2=new Thread(con);
        Thread t4=new Thread(con);
       
        t1.start();
       
        t3.start();
        t2.start();
        t4.start();
       
    }

}
根据教学视频写的代码怎么回事
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: color count 
2014-11-19 20:17
笔墨痕干
Rank: 1
等 级:新手上路
威 望:1
帖 子:56
专家分:0
注 册:2014-3-24
收藏
得分:0 
回复 2 楼 liao06550107
非常感谢,看瞎眼了,竟然把await()看成wait();谢谢!!!
2014-11-21 19:25
快速回复:线程问题,请帮忙看下,谢谢!
数据加载中...
 
   



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

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