use of com.cloud.utils.db.Transaction in project CloudStack-archive by CloudStack-extras.
the class TestAsyncJobManager method tstLocking.
public void tstLocking() {
int testThreads = 20;
Thread[] threads = new Thread[testThreads];
for (int i = 0; i < testThreads; i++) {
final int current = i;
threads[i] = new Thread(new Runnable() {
public void run() {
final HostDao hostDao = new HostDaoImpl();
while (true) {
Transaction txn = Transaction.currentTxn();
try {
HostVO host = hostDao.acquireInLockTable(getRandomLockId(), 10);
if (host != null) {
s_logger.info("Thread " + (current + 1) + " acquired lock");
try {
Thread.sleep(getRandomMilliseconds(1000, 5000));
} catch (InterruptedException e) {
}
s_logger.info("Thread " + (current + 1) + " released lock");
hostDao.releaseFromLockTable(host.getId());
try {
Thread.sleep(getRandomMilliseconds(1000, 5000));
} catch (InterruptedException e) {
}
} else {
s_logger.info("Thread " + (current + 1) + " is not able to acquire lock");
}
} finally {
txn.close();
}
}
}
});
threads[i].start();
}
try {
for (int i = 0; i < testThreads; i++) threads[i].join();
} catch (InterruptedException e) {
}
}
Aggregations