JUC学习笔记(5)—ReadWriteLockDemo

一个线程写入,100个线程读取

import java.util.concurrent.locks.ReentrantReadWriteLock;


class MyQueue
{
	private Object obj;
	private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();

	public void writeObj(Object obj)
	{
		rwLock.writeLock().lock();
		try 
		{
			this.obj = obj;
			System.out.println(Thread.currentThread().getName()+"\t"+obj);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			rwLock.writeLock().unlock();
		}
	}
	
	public void readObj()
	{
		rwLock.readLock().lock();
		try 
		{
			System.out.println(Thread.currentThread().getName()+"\t"+obj);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			rwLock.readLock().unlock();
		}
	}	
}


public class ReadWriteLockDemo
{
	public static void main(String[] args) throws InterruptedException
	{
		MyQueue q = new MyQueue();
		
		new Thread(() -> {
			q.writeObj("ClassName1018");
		}, "writeThread").start();
		
		Thread.sleep(100);
		
		for (int i = 1; i <=100; i++) 
		{
			new Thread(() -> {
				q.readObj();
			},String.valueOf(i)).start();
		}
		
	}
}

 

    原文作者:JUC
    原文地址: https://blog.csdn.net/java_kider/article/details/81321273
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞