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

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.await();//出错的地方,该方法通过signal()或者中断可以唤醒
            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.await();//出错的地方,wait()只能通过notigy()或notifyAll()方法唤醒
            // 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) {
                e.printStackTrace();
            }
    }
}

public class Test3 {

    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();
    }
}

听不同的音乐,看不同的书,游历不同的城市,邂逅不同的人,走的多了,站的高了,自然就看的远了。
2014-11-20 23:48
playyang123
Rank: 5Rank: 5
等 级:职业侠客
威 望:2
帖 子:70
专家分:332
注 册:2014-11-17
收藏
得分:3 
貌似不会
2014-11-21 11:35
笔墨痕干
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.049527 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved