生产-消费模式的synchronized和lock实现(十)

本文介绍使用ReentrantLock和synchronized实现的多生产者多消费者模式。通过两个示例展示了如何利用Java并发API实现线程间的同步和通信,确保在多线程环境下资源的安全访问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

lock:

package com.net.thread.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 
 * @Time:2017年8月23日 下午5:06:36
 * @version 1.0
 * @description 多生产/多消费,同步lock锁实现
 */
public class ReenTrantLockDemo3
{
    public static void main(String[] args)
    {
        ReadThread t = new ReadThread();
        for(int i = 0; i < 5; i++){
            new Thread(new Runnable()
            {
                @Override
                public void run()
                {
                    while(true)
                    {
                        t.printX();
                    }
                }
            }).start();
            
            new Thread(new Runnable()
            {
                @Override
                public void run()
                {
                    while(true)
                    {
                        t.printY();
                    }
                }
            }).start();
        }
    }

    static class ReadThread
    {
        private ReentrantLock lock = new ReentrantLock();
        private Condition condition = lock.newCondition();
        private boolean isFalgX = true;
        
        public void printX()
        {
            try {
                lock.lock();
                while(!isFalgX){
                    condition.await();
                }
                System.out.println(Thread.currentThread().getName() + "  +++++ " + System.currentTimeMillis());
                Thread.sleep(1000);
                isFalgX = false;
                condition.signalAll();
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                lock.unlock();
            }
        }
        
        public void printY()
        {
            try {
                lock.lock();
                while(isFalgX){
                    condition.await();
                }
                System.out.println(Thread.currentThread().getName() + "  ----- " + System.currentTimeMillis());
                Thread.sleep(1000);
                isFalgX = true;
                condition.signalAll();
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                lock.unlock();
            }
        }
    }

}

 

2、synchronized方法实现

package com.net.thread.synchonized;

/**
 * @author 
 * @Time:2017年8月18日 下午4:20:36
 * @version 1.0
 * @description   多生产生产/多消费模式,无限循环
 */
public class SynMethodDemo3 {

    public static void main(String[] args)
    {
        MyThread mt = new MyThread();
        for(int j = 0; j < 20; j++)
        {
            // put线程
            new Thread(new Runnable()
            {
                public void run()
                {
                    while(true)
                    {
                        mt.put();
                    }
                }
            }).start();

            // take线程
            new Thread(new Runnable()
            {
                public void run()
                {
                    while(true)
                    {
                        mt.take();
                    }
                }
            }).start();
        }
    }
    
    
    static class MyThread
    {
        private boolean isPut = true;
        public synchronized void put()
        {
            while (!isPut) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            for (int i = 0; i < 2; i++) {
                System.out.println("put方法 : ++++++++++++++++++++" + i + "  :  " + Thread.currentThread().getName());
            }
            
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
            isPut = false;
            this.notifyAll();
        }

        public synchronized void take()
        {
            while(isPut)
            {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            for (int i = 0; i < 2; i++) {
                System.out.println("take方法 :-------------------" + i  + "  :  " + Thread.currentThread().getName());
            }
            
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            isPut = true;
            this.notifyAll();
        }
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值