少年阿宾

那些青春的岁月

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

 

问题解决2——加入等待与唤醒


package edu.sjtu.erplab.thread;

class Info{
    private String name="name";
    private String content="content";
    private boolean flag=true;
    public  synchronized void set(String name,String content)
    {
        if(!flag)//标志位为false,不可以生产
        {
            try {
                super.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        this.setName(name);
        try {
            Thread.sleep(30);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        this.setContent(content);
        flag=false;//修改标志位为false,表示生产者已经完成资源,消费者可以消费。
        super.notify();//唤醒消费者进程
    }
   
    public synchronized void get()
    {
        if(flag)
        {
            try {
                super.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        try {
            Thread.sleep(30);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(this.getName()+":-->"+this.getContent());
        flag=true;//修改标志位为true,表示消费者拿走资源,生产者可以生产。
        super.notify();//唤醒生产者进程。
    }
   
   
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
   
}

class Producer implements Runnable{
    private Info info=null;
    public Producer(Info info)
    {
        this.info=info;
    }
   

    @Override
    public void run() {
        boolean flag=false;
        for(int i=0;i<10;i++)
            if(flag)
            {
                this.info.set("name+"+i, "content+"+i);
                flag=false;
            }
            else
            {
                this.info.set("name-"+i, "content-"+i);
                flag=true;
            }
    }
}

class Consumer implements Runnable{
    private Info info=null;
    public Consumer(Info info)
    {
        this.info=info;
    }
    @Override
    public void run() {
        for(int i=0;i<10;i++)
        {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            this.info.get();
        }
       
    }
}

public class ThreadDeadLock {
    public static void main(String args[])
    {
        Info info=new Info();
        Producer pro=new Producer(info);
        Consumer con=new Consumer(info);
        new Thread(pro).start();
        new Thread(con).start();
    }
   
}




http://www.cnblogs.com/xwdreamer/archive/2011/11/20/2296931.html#2397397

posted on 2012-08-02 15:30 abin 阅读(463) 评论(0)  编辑  收藏 所属分类: JavaMultithread

只有注册用户登录后才能发表评论。


网站导航: