();
private static final int THREAD_COUNT = 300;
private CountDownLatch latch = new CountDownLatch(THREAD_COUNT);
private CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT);
public void run() throws Exception {
StackTask stackTask = new StackTask();
long beginTime = System.currentTimeMillis();
for (int i = 0; i < THREAD_COUNT; i++) {
new Thread(stackTask).start();
}
latch.await();
long endTime = System.currentTimeMillis();
System.out.println("Stack consume Time: " + (endTime - beginTime) + " ms");
latch = new CountDownLatch(THREAD_COUNT);
barrier = new CyclicBarrier(THREAD_COUNT);
ConcurrentStackTask concurrentStackTask = new ConcurrentStackTask();
beginTime = System.currentTimeMillis();
for (int i = 0; i < THREAD_COUNT; i++) {
new Thread(concurrentStackTask).start();
}
latch.await();
endTime = System.currentTimeMillis();
System.out.println("ConcurrentStack consume Time: " + (endTime - beginTime) + " ms");
}
class StackTask implements Runnable {
@Override
public void run() {
try {
barrier.await();
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < 10; i++) {
stack.push(Thread.currentThread().getName());
stack.pop();
}
latch.countDown();
}
}
class ConcurrentStackTask implements Runnable {
@Override
public void run() {
try {
barrier.await();
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < 10; i++) {
concurrentStack.push(Thread.currentThread().getName());
concurrentStack.pop();
}
latch.countDown();
}
}
}
/*
Stack consume Time: 94 ms
ConcurrentStack consume Time: 63 ms
Stack consume Time: 78 ms
ConcurrentStack consume Time: 62 ms
*/Java性能调优笔记
原文:http://www.cnblogs.com/likehua/p/4087186.html