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