LockPerf benchmark and related fixes (try/break/continue interoperability)
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public class LockPerf {
|
||||
public static void main(String[] args) {
|
||||
int processors = Runtime.getRuntime().availableProcessors();
|
||||
for (int threadNum = 1; threadNum <= 1024; threadNum = threadNum < 2 * processors ? threadNum + 1 : threadNum * 2) {
|
||||
final AtomicInteger counter = new AtomicInteger();
|
||||
final CountDownLatch cdl = new CountDownLatch(threadNum);
|
||||
|
||||
final ReentrantLock lock = new ReentrantLock();
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
for (int i = 0; i < threadNum; ++i) {
|
||||
new Thread(new Runnable() {
|
||||
public void run() {
|
||||
for (;;) {
|
||||
lock.lock();
|
||||
try {
|
||||
if (counter.get() == 100000000) {
|
||||
cdl.countDown();
|
||||
break;
|
||||
} else {
|
||||
counter.incrementAndGet();
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
try {
|
||||
cdl.await();
|
||||
} catch (InterruptedException e) {//
|
||||
}
|
||||
System.out.println(threadNum + " " + (System.currentTimeMillis() - start));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
namespace lockperformance
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
fun thread(f: fun ()) {
|
||||
val thread = Thread(
|
||||
object: Runnable {
|
||||
override fun run() {
|
||||
f()
|
||||
}
|
||||
}
|
||||
)
|
||||
thread.start()
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val processors = Runtime.getRuntime().sure().availableProcessors()
|
||||
var threadNum = 1
|
||||
while(threadNum <= 1024) {
|
||||
val counter = AtomicInteger()
|
||||
val cdl = CountDownLatch(threadNum)
|
||||
val lock = ReentrantLock()
|
||||
|
||||
val start = System.currentTimeMillis()
|
||||
for(i in 0..threadNum-1) {
|
||||
thread {
|
||||
while(true) {
|
||||
lock.lock()
|
||||
try {
|
||||
if (counter.get() == 100000000) {
|
||||
cdl.countDown();
|
||||
break;
|
||||
} else {
|
||||
counter.incrementAndGet();
|
||||
}
|
||||
}
|
||||
finally {
|
||||
lock.unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cdl.await()
|
||||
|
||||
System.out?.println(threadNum.toString() + " " + (System.currentTimeMillis() - start));
|
||||
|
||||
if(threadNum < 2 * processors)
|
||||
threadNum = threadNum + 1
|
||||
else
|
||||
threadNum = threadNum * 2
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user