Java 多线程

Java 多线程

 

问题解决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

你可能感兴趣的:(Java 多线程)