test();
}
try{
barrier.await();
}catch(InterruptedExceptione){
e.printStackTrace();
}catch(BrokenBarrierExceptione){
e.printStackTrace();
}
}
});
}
try{
barrier.await();
}catch(InterruptedExceptione){
e.printStackTrace();
}catch(BrokenBarrierExceptione){
e.printStackTrace();
}
//所有线程执行完成之后,才会跑到这一步
longduration=System.currentTimeMillis()-start;
System.out.println(id+"="+duration);
}
protectedabstractvoidtest();
}
测试类ReentreLockTest源码
importthread.test.Test;
publicclassReentreLockTest{
privatestaticlongCOUNT=1000000;
privatestaticLocklock=newReentrantLock();
privatestaticlonglockCounter=0;
privatestaticlongsyncCounter=0;
privatestaticlongsemaCounter=0;
privatestaticAtomicLongatomicCounter=newAtomicLong(0);
privatestaticObjectsyncLock=newObject();
privatestaticSemaphoremutex=newSemaphore
(1);
publicstaticvoidtestLock(intnum,intthreadCount){
}
staticlonggetLock(){
lock.lock();
try{
returnlockCounter;
}finally{
lock.unlock();
}
}
staticlonggetSync(){
synchronized(syncLock){
returnsyncCounter;
}
}
staticlonggetAtom(){
returnatomicCounter.get();
}
staticlonggetSemaphore()throwsInterruptedException{
mutex.acquire();
try{
returnsemaCounter;
}finally{
mutex.release();
}
}
staticlonggetLockInc(){
lock.lock();
try{
return++lockCounter;
}finally{
lock.unlock();
}
}
staticlonggetSyncInc(){
synchronized(syncLock){
return++syncCounter;
}
}
staticlonggetAtomInc(){
returnatomicCounter.getAndIncrement();
}
staticclassSemaTestextendsTest{
publicSemaTest(Stringid,CyclicBarrierbarrier,longcount,
intthreadNum,ExecutorServiceexecutor){
super(id,barrier,count,threadNum,executor);
}
@Override
protectedvoidtest(){
try{
getSemaphore();
}catch(InterruptedExceptione){
e.printStackTrace();
}
}
}
staticclassLockTestextendsTest{
publicLockTest(Stringid,CyclicBarrierbarrier,longcount,
intthreadNum,ExecutorServiceexecutor){
super(id,barrier,count,threadNum,executor);
}
@Override
protectedvoidtest(){
getLock();
}
}
staticclassSyncTestextendsTest{
publicSyncTest(Stringid,CyclicBarrierbarrier,longcount,
intthreadNum,ExecutorServiceexecutor){
super(id,barrier,count,threadNum,executor);
}
@Override
protectedvoidtest(){
getSync();
}
}
staticclassAtomicTestextendsTest{
publicAtomicTest(Stringid,CyclicBarrierbarrier,longcount,
intthreadNum,ExecutorServiceexecutor){
super(id,barrier,count,threadNum,executor);
}
@Override
protectedvoidtest(){
getAtom();
}
}
publicstaticvoidtest(Stringid,longcount,intthreadNum,
ExecutorServiceexecutor){
finalCyclicBarrierbarrier=newCyclicBarrier(threadNum+1,
newThread(){
@Override
publicvoidrun(){
}
});
System.out.println("==============================");
System.out.println("count="+count+"/t"+"ThreadCount="
+threadNum);
newLockTest("Lock",barrier,COUNT,threadNum,executor).startTest();
newSyncTest("Sync",barrier,COUNT,threadNum,executor).startTest();
newAtomicTest("Atom",barrier,COUNT,threadNum,executor)
.startTest();
newSemaTest("Sema",barrier,COUNT,threadNum,executor)
.startTest();
System.out.println("==============================");
}
publicstaticvoidmain(String[]args){
for(inti=1;i<5;i++){
ExecutorServiceexecutor=Executors.newFixedThreadPool(10*i);
test("",COUNT*i,10*i,executor);
}
}
}
结果
==============================
count=1000000ThreadCount=10
Lock=953
Sync=3781
Atom=78
Sema=4922
==============================
==============================
count=2000000ThreadCount=20
Lock=1906
Sync=8469
Atom=172
Sema=9719
==============================
==============================
count=3000000ThreadCount=30
Lock=2890
Sync=12641
Atom=219
Sema=15015
==============================
==============================
count=4000000ThreadCount=40
Lock=3844
Sync=17141
Atom=343
Sema=19782
packagetest.thread;
importstaticjava.lang.System.out;
importjava.util.Random;
importjava.util.concurrent.BrokenBarrierException;
importjava.util.concurrent.CyclicBarrier;
importjava.util.concurrent.ExecutorService;
importjava.util.concurrent.Executors;
importjava.util.concurrent.atomic.AtomicInteger;
importjava.util.concurrent.atomic.AtomicLong;
importjava.util.concurrent.locks.ReentrantLock;
publicclassTestSyncMethods{
publicstaticvoidtest(intround,intthreadNum,CyclicBarriercyclicBarrier){
newSyncTest("Sync",round,threadNum,cyclicBarrier).testTime();
newLockTest("Lock",round,threadNum,cyclicBarrier).testTime();
newAtomicTest("Atom",round,threadNum,cyclicBarrier).testTime();
}
publicstaticvoidmain(Stringargs[]){
for(inti=0;i<5;i++){
intround=100000*(i+1);
intthreadNum=5*(i+1);
CyclicBarriercb=newCyclicBarrier(threadNum*2+1);
out.println("==========================");
out.println("round:
"+round+"thread:
"+threadNum);
test(round,threadNum,cb);
}
}
}
classSyncTestextendsTestTemplate{
publicSyncTest(String_id,int_round,int_threadNum,CyclicBarrier_cb){
super(_id,_round,_threadNum,_cb);
}
@Override
/**
*synchronized关键字不在方法签名里面,所以不涉及重载问题
*/
synchronizedlonggetValue(){
returnsuper.countValue;
}
@Override
synchronizedvoidsumValue(){
super.countValue+=preInit[index++%round];
}
}
classLockTestextendsTestTemplate{
ReentrantLocklock=newReentrantLock();
publicLockTest(String_id,int_round,int_threadNum,CyclicBarrier_cb){
super(_id,_round,_threadNum,_cb);
}
/**
*synchronized关键字不在方法签名里面,所以不涉及重载问题
*/
@Override
longgetValue(){
try{
lock.lock();
returnsuper.countValue;
}finally{
lock.unlock();
}
}
@Override
voidsumValue(){
try{
lock.lock();
super.countValue+=preInit[index++%round];
}finally{
lock.unlock();
}
}
}
classAtomicTestextendsTestTemplate{
publicAtomicTest(String_id,int_round,int_threadNum,CyclicBarrier_cb){
super(_id,_round,_threadNum,_cb);
}
@Override
/**
*synchronized关键字不在方法签名里面,所以不涉及重载问题
*/
longgetValue(){
returnsuper.countValueAtmoic.get();
}
@Override
voidsumValue(){
super.countValueAtmoic.addAndGet(super.preInit[indexAtomic.get()%round]);
}
}
abstractclassTestTemplate{
privateStringid;
protectedintround;
privateintthreadNum;
protectedlongcountValue;
protectedAtomicLongcountValueAtmoic=newAtomicLong(0);
protectedint[]preInit;
protectedintindex;
protectedAtomicIntegerindexAtomic=newAtomicInteger(0);
Randomr=newRandom(47);
//任务栅栏,同批任务,先到达wait的任务挂起,一直等到全部任务到达制定的wait地点后,才能全部唤醒,继续执行
privateCyclicBarriercb;
publicTestTemplate(String_id,int_round,int_threadNum,CyclicBarrier_cb){
this.id=_id;
this.round=_round;
this.threadNum=_threadNum;
cb=_cb;
preInit=newint[round];
for(inti=0;ipreInit[i]=r.nextInt(100);
}
}
abstractvoidsumValue();
/*
*对long的操作是非原子的,原子操作只针对32位
*long是64位,底层操作的时候分2个32位读写,因此不是线程安全
*/
abstractlonggetValue();
publicvoidtestTime(){
ExecutorServicese=Executors.newCachedThreadPool();
longstart=System.nanoTime();
//同时开启2*ThreadNum个数的读写线程
for(inti=0;ise.execute(newRunnable(){
pub