共享队列ShareQueue

public class SharedQueue {
	class Node {
		Object task;
		Node next;
		
		Node(Object task) {
			this.task = task;
			next = null;
		}
	}
	
	private Node head = new Node(null);
	private Node last = head;
	private volatile int w;
	private Object putLock = new Object();
	private Object takeLock = new Object();
	
	public void put(Object task) {
		System.out.println("...... " + Thread.currentThread().getName() + " wants to get the  putLock....");
		synchronized (putLock) {
			System.out.println("...... " + Thread.currentThread().getName() + " gets putLock....");
			Node p = new Node(task);
			last.next = p;
			last = p;
			System.out.println("...... putting "+ task);
			if (w > 0) {
				System.out.println("...... w = " + w);
				putLock.notify();
			}
		}
	}
	
	public Object take() {
		Object task = null;
		System.out.println(Thread.currentThread().getName() + " wants to get the  takeLock....");
		synchronized (takeLock) {
			System.out.println(Thread.currentThread().getName() + " gets takeLock....");
			while (isEmpty()) {
				try {
					System.out.println(Thread.currentThread().getName() + " want to get putLock....");
					synchronized (putLock) {
						System.out.println(Thread.currentThread().getName() + " gets putLock....");
						w++;
						putLock.wait();
						w--;
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
			{
				Node first = head.next;
				task = first.task;
				first.task = null;
				head = first;
			}
		}
		System.out.println(Thread.currentThread().getName() + " releases takeLock....");
		return task;
	}
	
	private boolean isEmpty() {
		return head.next == null;
	}
	
	public static void main(String[] args) {
		final SharedQueue queue = new SharedQueue();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i = 0; i < 1000; i++) {
					queue.put(Integer.valueOf(i));
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i = 0; i < 1000; i++) {
					try {
						Thread.sleep(300);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("============taking " + queue.take());
				}
			}
		}).start();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i = 0; i < 1000; i++) {
					try {
						Thread.sleep(300);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("----------------taking " + queue.take());
				}
			}
		}).start();

	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C++ 中实现多线程共享队列可以使用标准库中的 std::queue 和 std::mutex 来实现。具体实现步骤如下: 1. 定义一个模板类,用于表示队列中的元素类型; 2. 在队列类中定义两个 std::queue 成员变量,一个用于存储任务,另一个用于存储等待的线程; 3. 定义一个 std::mutex 对象,用于保护队列的数据; 4. 在队列类中定义 push() 和 pop() 函数,这两个函数需要使用 std::lock_guard<std::mutex> 对象来保护队列的数据; 5. 在 push() 函数中向任务队列中添加任务,并通知等待的线程有新任务加入; 6. 在 pop() 函数中从任务队列中取出一个任务并返回,如果队列为空则等待新任务的到来。 下面是一个简单的多线程共享队列的实现: ```c++ #include <queue> #include <mutex> #include <condition_variable> template<typename T> class ThreadSafeQueue { public: ThreadSafeQueue() = default; void push(const T& value) { std::lock_guard<std::mutex> lock(m_mutex); m_queue.push(value); m_condition.notify_one(); } bool pop(T& value) { std::unique_lock<std::mutex> lock(m_mutex); m_condition.wait(lock, [this]{ return !m_queue.empty(); }); if (m_queue.empty()) { return false; } value = m_queue.front(); m_queue.pop(); return true; } private: std::queue<T> m_queue; mutable std::mutex m_mutex; std::condition_variable m_condition; }; ``` 在上面的代码中,我们使用 std::unique_lock<std::mutex> 来保护队列的数据,并使用 std::condition_variable 来通知等待的线程有新任务加入。在 push() 函数中,我们先获取 std::lock_guard<std::mutex> 对象来保护队列的数据,然后向任务队列中添加任务,并通过 m_condition.notify_one() 通知等待的线程有新任务加入。在 pop() 函数中,我们获取 std::unique_lock<std::mutex> 对象来保护队列的数据,并通过 m_condition.wait() 等待新任务的到来。如果队列中有任务,则取出一个任务并返回,否则返回 false。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值