学习日记1105--多线程间的通信(生产者消费者)(1)

<p>今天主要学习了多线程间的通信,并对毕老师所讲的内容作了认真的理解分析,内容整理如下: </p><p>在生产者消费者程序运行过程中,可能出现生产两个消费一个或者生产一个消费两个的情况,如下图所示。对其产生原因进行分析,并指出改进方法。</p><p><img src="https://img-blog.csdn.net/20141105202626609?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDM3ODMyMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></p><p><span style="font-family: Arial, Helvetica, sans-serif;">生产者消费者两个进程中的同步代码如下:</span></p><p><span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="java">public synchronized void input(String name)
	{
		if(flag)
			try
			{
				this.wait();
			}
			catch (Exception e)
			{
			}
		this.name = name + count++;
		System.out.println(Thread.currentThread().getName()+"...生产者"+this.name);
		flag = true;
		this.notify();
	}

	public synchronized void out()
	{
		if(!flag)
			try
			{
				this.wait();
			}
			catch (Exception e)
			{
			}
		System.out.println(Thread.currentThread().getName()+"-----------消费者"+this.name);
		flag = false;
		notify();
	}

对其执行过程进行分析:

根本原因:唤醒本方线程,且t1唤醒t2时,直接从等待位置,直接向下执行而未判断标记。

解决办法:(1)循环判断标记,将if条件判断,改为while循环判断

          (2)唤醒对方线程,将notify()(仅唤醒线程池中的一个线程)改为notifyAll()(唤醒线程池中的所有线程)

改进后的代码如下:

public synchronized void input(String name)
	{
		while(flag)
			try
			{
				this.wait();
			}
			catch (Exception e)
			{
			}
		this.name = name + count++;
		System.out.println(Thread.currentThread().getName()+"...生产者"+this.name);
		flag = true;
		this.notifyAll();
	}

	public synchronized void out()
	{
		while(!flag)
			try
			{
				this.wait();
			}
			catch (Exception e)
			{
			}
		System.out.println(Thread.currentThread().getName()+"-----------消费者"+this.name);
		flag = false;
		notifyAll();
	}

接下来是JDK1.5中对多线程的升级方案




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值