码迷,mamicode.com
首页 > 编程语言 > 详细

多线程间的通信问题之交替打印

时间:2015-05-20 13:18:44      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:多线程通信

1.要实现的效果,直接上图:

技术分享

1.具体逻辑很清晰,就是通过多线程来实现直接上代码,lock控制相同的输入或输出线程的同步,resource控制着输入和输出线程的同步

class Resource
{
	private String name;
	private String sex;
	private boolean flag;
	public void setName(String name)
	{
		this.name=name;
	}
	public void setSex(String sex)
	{
		this.sex=sex;
	}
	public void setFlag(boolean flag)
	{
		this.flag=flag;
	}
	public String getName()
	{
		return this.name;
	}
	public String getSex()
	{
		return this.sex;
	}
	public boolean getFlag()
	{
		return this.flag;
	}
}
class Input implements Runnable
{
	private static final Object lock=new Object();
	int num=0;
	Resource resource;
	public Input(Resource resource)
	{
		this.resource=resource;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (!Thread.interrupted()) {//调用interrupt方法时终止线程
			synchronized (lock) {
				synchronized (resource) {
					if(resource.getFlag())
					{
						try {
							resource.wait();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					if((num&1)==0)//用奇数偶数来实现交替输出
					{
						resource.setSex("male");
						resource.setName("mnmlist");
					}else{
						resource.setSex("female");
						resource.setName("sting");
					}
					num++;
					if(num>10000)
						Thread.currentThread().interrupt();
					resource.setFlag(true);
					resource.notify();
				}
			}
		}
	}
	
}
class Output implements Runnable
{
	private static final Object lock=new Object();
	int num=0;
	Resource resource;
	public Output(Resource resource)
	{
		this.resource=resource;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (!Thread.interrupted()) {
			synchronized (lock) {
				synchronized (resource) {
					if(!resource.getFlag())
					{
						try {
							resource.wait();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					System.out.println(Thread.currentThread().getName()+","+(++num)+"th,姓名:"+resource.getName()+",性别:"+resource.getSex());
					if(num>10000)
						Thread.currentThread().interrupt();
					resource.setFlag(false);
					resource.notify();
				}
			}
		}
	}
	
}
public class OneAfterAnotherOutput {

	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Resource resource=new Resource();
		Input input=new Input(resource);
		Output output=new Output(resource);
		Input input1=new Input(resource);
		Output output1=new Output(resource);
		Thread inputThread=new Thread(input);
		Thread outputThread=new Thread(output);
		Thread inputThread1=new Thread(input1);
		Thread outputThread1=new Thread(output1);
		inputThread.start();
		outputThread.start();
		inputThread1.start();
		outputThread1.start();
	}

}


多线程间的通信问题之交替打印

标签:多线程通信

原文地址:http://blog.csdn.net/mnmlist/article/details/45868221

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!