请教一个java线程通讯问题

今天写了一个java线程通讯的例子,两段代码只是有无else的问题,但执行效果却不同,想不明白为什么,还请高手赐教

代码1:

//资源
class Resource
{
    private String name;
    private String sex;
    private boolean flag;//true:已赋值完成,应该输出;false:已输出需要新赋值

    public synchronized void set(String name, String sex)
    {
        if(this.flag)
	{
	    try
	    {
	        this.wait();
	    }
	    catch(InterruptedException e)
	    {}
	}
	//else {
	this.name = name;
	this.sex = sex;
	this.flag = true;
	this.notify();
	//}
    }

    public synchronized void out()
    {
        if(!this.flag)
	{
	    try
	    {
	        this.wait();
	    }
	    catch(InterruptedException e)
	    {}
	}
	//else {
	System.out.println(this.name + ",性别 :" + this.sex);
	this.flag = false;
	this.notify();
	//}
    }
}

//任务1 - 给资源赋值
class Task1Input implements Runnable
{
    Resource resource;

    public Task1Input(Resource resource)
    {
        this.resource = resource;
    }

    public void run()
    {
	int syscle = 0;
        while(true)
	{
	    if(syscle == 0)
            {
	        resource.set("Jack", "man");
	    }
            else
	    {
		resource.set("露丝", "女");
	    }
	    syscle = (syscle+1)%2;
	}
    }
}

//任务2 - 输出资源
class Task2Output implements Runnable
{
    Resource resource;

    public Task2Output(Resource resource)
    {
        this.resource = resource;
    }

    public void run()
    {
        while(true)
	{
	    resource.out();
	}
    }
}

class ThreadCommunication
{
    public static void main(String[] args)
    {
	//统一的资源
        Resource resource = new Resource();

	//不同的任务
        Task1Input task1 = new Task1Input(resource);
        Task2Output task2 = new Task2Output(resource);

        Thread thread1 = new Thread(task1);
	Thread thread2 = new Thread(task2);

	thread1.start();
	thread2.start();
    }
}

 执行结果是 “Jack,性别 :man” 和 “露丝,性别 :女”  交替循环打印。

 

代码2:

//资源
class Resource
{
    private String name;
    private String sex;
    private boolean flag;//true:已赋值完成,应该输出;false:已输出需要新赋值

    public synchronized void set(String name, String sex)
    {
        if(this.flag)
	{
	    try
	    {
	        this.wait();
	    }
	    catch(InterruptedException e)
	    {}
	}
	else {
	    this.name = name;
	    this.sex = sex;
	    this.flag = true;
	    this.notify();
	}
    }

    public synchronized void out()
    {
        if(!this.flag)
	{
	    try
	    {
	        this.wait();
	    }
	    catch(InterruptedException e)
	    {}
	}
	else {
	    System.out.println(this.name + ",性别 :" + this.sex);
	    this.flag = false;
	    this.notify();
	}
    }
}

//任务1 - 给资源赋值
class Task1Input implements Runnable
{
    Resource resource;

    public Task1Input(Resource resource)
    {
        this.resource = resource;
    }

    public void run()
    {
	int syscle = 0;
        while(true)
	{
	    if(syscle == 0)
            {
	        resource.set("Jack", "man");
	    }
            else
	    {
		resource.set("露丝", "女");
	    }
	    syscle = (syscle+1)%2;
	}
    }
}

//任务2 - 输出资源
class Task2Output implements Runnable
{
    Resource resource;

    public Task2Output(Resource resource)
    {
        this.resource = resource;
    }

    public void run()
    {
        while(true)
	{
	    resource.out();
	}
    }
}

class ThreadCommunication
{
    public static void main(String[] args)
    {
	//统一的资源
        Resource resource = new Resource();

	//不同的任务
        Task1Input task1 = new Task1Input(resource);
        Task2Output task2 = new Task2Output(resource);

        Thread thread1 = new Thread(task1);
	Thread thread2 = new Thread(task2);

	thread1.start();
	thread2.start();
    }
}

 执行结果却不是交替打印,不知什么原因,请赐教。

 

 

--明白了:线程在行代码中断,就在那行唤醒。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值