java多线程wait,notify浅析

对于java多线程相信很多人都应该很了解了,那么对于java多线程的wait,notify你又了解多少呢?一起通过下面的内容对这方面进行一下了解吧。

注意:

涉及到了对象锁,Wait、Notify一定要在synchronized里面进行使用;Wait必须暂定当前正在执行的线程,并释放资源锁,让其他线程可以有机会运行;notify/notifyall: 唤醒线程。

共享变量

public class ShareEntity
{
    private String name;
    // 线程通讯标识
    private Boolean flag = false;
    public ShareEntity()
    {}
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public Boolean getFlag()
    {
        return flag;
    }
    public void setFlag(Boolean flag)
    {
        this.flag = flag;
    }
}

线程1(生产者)

public class CommunicationThread1 extends Thread
{
    private ShareEntity shareEntity;
    public CommunicationThread1(ShareEntity shareEntity)
    {
        this.shareEntity = shareEntity;
    }
    @Override
    public void run()
    {
        int num = 0;
        while (true)
        {
            synchronized(shareEntity)
            {
                if (shareEntity.getFlag())
                {
                    try
                    {
                        shareEntity.wait();
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
                if (num % 2 == 0)
                    shareEntity.setName("thread1-set-name-0");
                else
                    shareEntity.setName("thread1-set-name-1");
                num++;
                shareEntity.setFlag(true);
                shareEntity.notify();
            }
        }
    }
}

线程2(消费者)

public class CommunicationThread2 extends Thread
{
    private ShareEntity shareEntity;
    public CommunicationThread2(ShareEntity shareEntity)
    {
        this.shareEntity = shareEntity;
    }
    @Override
    public void run()
    {
        while (true)
        {
            synchronized(shareEntity)
            {
                if (!shareEntity.getFlag())
                {
                    try
                    {
                        shareEntity.wait();
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
                System.out.println(shareEntity.getName());
                shareEntity.setFlag(false);
                shareEntity.notify();
            }
        }
    }
}

请求

@RequestMapping("test-communication")
public void testCommunication()
{
    ShareEntity shareEntity = new ShareEntity();
    CommunicationThread1 thread1 = new CommunicationThread1(shareEntity);
    CommunicationThread2 thread2 = new CommunicationThread2(shareEntity);
    thread1.start();
    thread2.start();
}

结果

thread1 - set - name - 0
thread1 - set - name - 1
thread1 - set - name - 0
thread1 - set - name - 1
thread1 - set - name - 0
thread1 - set - name - 1
thread1 - set - name - 0

以上就是对于java多线程wait,notify的简单介绍了,更多java常见问题及解决方法,可以继续通过奇Q工具网来了解哦。

推荐阅读:

java多线程有几种实现方法?java多线程实现的几种方式分享

java多线程代码实现,多线程简单例子分享

java多线程实战,java多线程经典案例