共享队列ShareQueue

本文介绍了一个并发共享队列的实现,包括队列的基本操作:put和take,并通过多线程的方式展示了如何高效地进行数据的存取。通过同步锁的使用,确保了线程安全,实现了在并发环境下对数据的可靠操作。

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

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();

	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值