java基础—线程间的通讯 生产者与消费者



线程间的的通讯  生产者与消费者

public class TestDemos3
{
	public static void main(String[] args)
	{
		Res  r = new Res();
		
		Input in = new Input(r);
		
		Output out = new Output(r);
		
		Thread t1 = new Thread(in);
		Thread t2 = new Thread(out);
		
		t1.start();
		t2.start();
	}
	}

class Res
{
	String name;
	String sex;
	}

class Input  implements Runnable
{
	private Object obj;
	private Res r;
	Input(Res r)
	{ 
		this .r = r;
	}
	public void run()
	{
		 boolean b = false;
		while(true)
		{
		    synchronized(Input.class)
		    {
		       if(b)
		       {
		    	    r.name="张三";
		    	    r.sex="男";
		    	    b=false;
		       }
		       else
		       {
		    	   r.name = "kk";
		    	   r.sex = "woman";
		    	    b=true;
		       }
		    }
		}
	}
	}
class Output implements Runnable

{
    private Res r;
    private Object obj;
    Output(Res r)
    {
    	this.r = r;
    }
    public void run()
    {
    	while(true)
    	{
    		synchronized(Input.class)
    		{
    		    System.out.println(Thread.currentThread().getName()+r.name+": "+r.sex);
    	    }
    	}
    }
}





等待唤醒机制

public class TestDemos3
{
	public static void main(String[] args)
	{
		Res  r = new Res();
		
		Input in = new Input(r);
		
		Output out = new Output(r);
		
		Thread t1 = new Thread(in);
		Thread t3 = new Thread(in);
		Thread t2 = new Thread(out);
		Thread t4 = new Thread(out);
		
		t1.start();
		t2.start();
		t3.start();
		t4.start();
	}
	}

class Res
{
	String name;
	String sex;
	boolean flag = false;
	}

class Input  implements Runnable
{
	private Object obj;
	private Res r;
	
	Input(Res r)
	{ 
		this .r = r;
	}
	public void run()
	{
		 boolean b = false;
		while(true)
		{
		    synchronized(r)
		    {
		    	if(r.flag)
		    	{
					try {
						  r.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
		    	}
		       if(b)
		       {
		    	    r.name="张三";
		    	    r.sex="男";
		    	    b=false;
		       }
		       else
		       {
		    	   r.name = "kk";
		    	   r.sex = "woman";
		    	    b=true;
		       }
		       r.flag=true;
		       r.notify();
		    }
		}
	}
	}
class Output implements Runnable

{
    private Res r;
    private Object obj;
    Output(Res r)
    {
    	this.r = r;
    }
    public void run()
    {
    	while(true)
    	{
    		synchronized(r)
    		{
    			if(!r.flag)
    			{
					try {
						r.wait();
					} catch (InterruptedException e) 
					{
						e.printStackTrace();
					}
    			}
    		    System.out.println(Thread.currentThread().getName()+r.name+": "+r.sex);
    	            r.flag=false;
    	            r.notify();
    		}
    	}
    }
}


运行程序:


java基础—线程间的通讯 生产者与消费者_第1张图片



多线程演示生产者与消费者示例


public class ProduceConsumDemos 
{

	public static void main(String[] args) 
	{
		Ress r = new Ress();
		
		Inputs in = new Inputs(r);
		Outputs out = new Outputs(r);
		
		Thread t = new Thread(in);
		Thread t1 = new Thread(in);
		
		Thread t2 = new Thread(out);
		Thread t3 = new Thread(out);
		
		t.start();
		t1.start();
		t2.start();
		t3.start();

	}

}

class Ress
{
	private String name;
	private boolean blag = false;
	private int count=0;
	
	public synchronized void setInput(String name )
	{
		while(blag)
		{
			try 
			{
				 wait();
			} catch (InterruptedException e) 
			{
				e.printStackTrace();
			}
		}
		
		this.name = name+"--"+count++;
		System.out.println(Thread.currentThread().getName()+"生产者-"+this.name);
		blag=true;
		this.notifyAll();
		
	}
	
	public synchronized void out()
	{
		while(!blag)
		{
			try 
			{
				wait();
			} catch (InterruptedException e) 
			{
				e.printStackTrace();
			}
		}
		
		System.out.println(Thread.currentThread().getName()+"消费者---"+this.name);
		blag=false;
		this.notifyAll();
		
	}
	}

class Inputs implements Runnable
{
	private Ress r;
	
	Inputs(Ress r)
	{
		this.r=r;
	}
	public void run()
	{
	
		while(true)
		{
				r.setInput("商品");
		}
	}
}

class Outputs implements Runnable
{
	private Ress r;
	Outputs(Ress r)
	{
		this.r = r;
	}
	public void run()
	{
		while(true)
		    r.out();
	}
}












java基础—线程间的通讯 生产者与消费者_第2张图片



加入Lock

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


public class Test3 
{

	public static void main(String[] args)
	{
		System.out.println("text3启动");
		Resii r = new Resii();
		
		Inputii in = new Inputii(r);
		Outputii out = new Outputii(r);
		
		Thread t1 = new Thread(in);
		Thread t2 = new Thread(in);
		Thread t3 = new Thread(out);
		Thread t4 = new Thread(out);
		t1.start();
		t2.start();
		t3.start();
		t4.start();
	}
}
class Resii
{
	private String name ;
	private boolean flag = false;
	private int count=0;

	private Lock lock = new ReentrantLock();
	private Condition conin = lock.newCondition();
	private Condition conout = lock.newCondition();
	public void set(String name )
	{
		lock.lock();
		while(flag)
		{
			try 
			{
				conin.await();
			} catch (InterruptedException e) 
			{
				e.printStackTrace();
			}
		}
		this.name = name+" :"+count++;
		System.out.println(Thread.currentThread().getName()+"生产者 "+this.name);
		flag = true;
		conout.signal();
		lock.unlock();
	}
	public void out()
	{
		lock.lock();
		while(!flag)
		{
			try 
			{
				conout.await();
			} catch (InterruptedException e) 
			{
				e.printStackTrace();
			}
		}
		System.out.println(Thread.currentThread().getName()+"消费者________"+this.name);
		flag = false;
		conin.signal();
		lock.unlock();
	}
}

class Inputii implements Runnable
{
	private Resii r;
	Inputii(Resii r)
	{
		this.r=r;
	}
	public void run()
	{
		while (true)
		{
			r.set("商品");
		}
	}
}

class Outputii implements Runnable
{
   private Resii r;
   Outputii(Resii r)
   {
	   this.r = r;
   }
   public void run()
   {
	   while (true)
	   {
		   r.out();
	   }
   }
}












你可能感兴趣的:(多线程,java基础,线程,通讯,黑马程序员)