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


运行程序:





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


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















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












早起的年轻人 CSDN认证博客专家 移动开发 项目管理 Java
只要用心去做,每一件事情还是有可能成功的,当然成功是没有界限的,只不过是达到自己心里的那个目标,公众号:我的大前端生涯,一个爱喝茶的程序员,通常会搞搞SpringBoot 、Herbinate、Mybatiys、Android、iOS、Flutter、Vue、小程序等.
©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页