java读锁在耗时_Java 中锁之间的对比

packageconstxiong.concurrency.a020;importjava.util.ArrayList;importjava.util.List;importjava.util.UUID;importjava.util.concurrent.locks.Lock;importjava.util.concurrent.locks.ReadWriteLock;importjava.util.concurrent.locks.ReentrantLock;importjava.util.concurrent.locks.ReentrantReadWriteLock;/*** JDK 1.8 中锁性能的测试

*@authorConstXiong*/

public classTestLockPerformance {public static Object obj = new Object();//用于 synchronized 获取锁

public static Lock lock = new ReentrantLock();//可重入锁

public static ReadWriteLock readWriteLock = new ReentrantReadWriteLock();//读写锁

public static final int READ = 0;public static final int WRITE = 1;//uuid,一个随机字符串

public static String uuid =UUID.randomUUID().toString();public static void main(String[] args) throwsInterruptedException {//testSynchronized(1000);

testReentrantLock(1000);//testReadWriteLock(1000);

}public static void testSynchronized(int threadNum) throwsInterruptedException {long t1 =System.currentTimeMillis();

List tList = new ArrayList();//启动 threadNum - 向上取整 (0.01 * threadNum) 个线程读 uuid, 向上取整 (0.01 * threadNum) 个线程写 uuid

for (int i = 0; i < threadNum; i++) {

Thread t;if (i % 100 == 0) {

t= new Thread(newWorkerSynchronized(WRITE));

}else{

t= new Thread(newWorkerSynchronized(READ));

}

t.start();//启动线程

tList.add(t);

}for(Thread t : tList) {

t.join();

}long t2 =System.currentTimeMillis();

System.out.println("testSynchronized 耗时:" + (t2 -t1));

}public static void testReentrantLock(int threadNum) throwsInterruptedException {long t1 =System.currentTimeMillis();

List tList = new ArrayList();//启动 threadNum - 向上取整 (0.01 * threadNum) 个线程读 uuid, 向上取整 (0.01 * threadNum) 个线程写 uuid

for (int i = 0; i < threadNum; i++) {

Thread t;if (i % 100 == 0) {

t= new Thread(newWorkerReentrantLock(WRITE));

}else{

t= new Thread(newWorkerReentrantLock(READ));

}

t.start();//启动线程

tList.add(t);

}for(Thread t : tList) {

t.join();

}long t2 =System.currentTimeMillis();

System.out.println("testReentrantLock 耗时:" + (t2 -t1));

}public static void testReadWriteLock(int threadNUm) throwsInterruptedException {long t1 =System.currentTimeMillis();

List tList = new ArrayList();//启动 threadNum - 向上取整 (0.01 * threadNum) 个线程读 uuid, 向上取整 (0.01 * threadNum) 个线程写 uuid

for (int i = 0; i < threadNUm; i++) {

Thread t;if (i % 100 == 0) {

t= new Thread(newWorkerReadWriteLock(WRITE));

}else{

t= new Thread(newWorkerReadWriteLock(READ));

}

t.start();//启动线程

tList.add(t);

}for(Thread t : tList) {

t.join();

}long t2 =System.currentTimeMillis();

System.out.println("testReadWriteLock 耗时:" + (t2 -t1));

}

}//工作线程,使用 synchronized 关键字加锁

class WorkerSynchronized implementsRunnable {//0-read;1-write

private inttype;

WorkerSynchronized(inttype) {this.type =type;

}//加锁读 TestLockPerformance.uuid 变量,并打印

private voidread() {synchronized(TestLockPerformance.obj) {//休眠 20 毫秒,模拟任务执行耗时

try{

Thread.sleep(20);

}catch(InterruptedException e) {

e.printStackTrace();

}

System.out.println(Thread.currentThread().getName()+

" read uuid = " +TestLockPerformance.uuid);

}

}//加锁写 TestLockPerformance.uuid 变量,并打印

private voidwrite() {synchronized(TestLockPerformance.obj) {//休眠 20 毫秒,模拟任务执行耗时

try{

Thread.sleep(20);

}catch(InterruptedException e) {

e.printStackTrace();

}

TestLockPerformance.uuid=UUID.randomUUID().toString();

System.out.println(Thread.currentThread().getName()+

" write uuid = " +TestLockPerformance.uuid);

}

}

@Overridepublic voidrun() {//type = 0,线程读 TestLockPerformance.uuid 变量

if (type == 0) {

read();//type = 1,线程生成 uuid,写入 TestLockPerformance.uuid 变量

} else{

write();

}

}

}//工作线程,使用 ReentrantLock 加锁

class WorkerReentrantLock implementsRunnable {//0-read;1-write

private inttype;

WorkerReentrantLock(inttype) {this.type =type;

}//加锁读 TestLockPerformance.uuid 变量,并打印

private voidread() {

TestLockPerformance.lock.lock();try{//休眠 20 毫秒,模拟任务执行耗时

try{

Thread.sleep(20);

}catch(InterruptedException e) {

e.printStackTrace();

}

System.out.println(Thread.currentThread().getName()+

" read uuid = " +TestLockPerformance.uuid);

}finally{

TestLockPerformance.lock.unlock();

}

}//加锁写 TestLockPerformance.uuid 变量,并打印

private voidwrite() {

TestLockPerformance.lock.lock();try{//休眠 20 毫秒,模拟任务执行耗时

try{

Thread.sleep(20);

}catch(InterruptedException e) {

e.printStackTrace();

}

TestLockPerformance.uuid=UUID.randomUUID().toString();

System.out.println(Thread.currentThread().getName()+

" write uuid = " +TestLockPerformance.uuid);

}finally{

TestLockPerformance.lock.unlock();

}

}

@Overridepublic voidrun() {//type = 0,线程读 TestLockPerformance.uuid 变量

if (type == 0) {

read();//type = 1,线程生成 uuid,写入 TestLockPerformance.uuid 变量

} else{

write();

}

}

}//工作线程,使用 ReentrantReadWriteLock 关键字加锁

class WorkerReadWriteLock implementsRunnable {//0-read;1-write

private inttype;

WorkerReadWriteLock(inttype) {this.type =type;

}//加锁读 TestLockPerformance.uuid 变量,并打印

private voidread() {

TestLockPerformance.readWriteLock.readLock().lock();try{//休眠 20 毫秒,模拟任务执行耗时

try{

Thread.sleep(20);

}catch(InterruptedException e) {

e.printStackTrace();

}

System.out.println(Thread.currentThread().getName()+

" read uuid = " +TestLockPerformance.uuid);

}finally{

TestLockPerformance.readWriteLock.readLock().unlock();

}

}//加锁写 TestLockPerformance.uuid 变量,并打印

private voidwrite() {

TestLockPerformance.readWriteLock.writeLock().lock();try{//休眠 20 毫秒,模拟任务执行耗时

try{

Thread.sleep(20);

}catch(InterruptedException e) {

e.printStackTrace();

}

TestLockPerformance.uuid=UUID.randomUUID().toString();

System.out.println(Thread.currentThread().getName()+

" write uuid = " +TestLockPerformance.uuid);

}finally{

TestLockPerformance.readWriteLock.writeLock().unlock();

}

}

@Overridepublic voidrun() {//type = 0,线程读 TestLockPerformance.uuid 变量

if (type == 0) {

read();//type = 1,线程生成 uuid,写入 TestLockPerformance.uuid 变量

} else{

write();

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值