use of org.h2.util.Task in project h2database by h2database.
the class TestCacheConcurrentLIRS method testConcurrent.
private void testConcurrent() {
CacheLongKeyLIRS.Config cc = new CacheLongKeyLIRS.Config();
cc.maxMemory = 100;
final CacheLongKeyLIRS<Integer> test = new CacheLongKeyLIRS<>(cc);
int threadCount = 8;
final CountDownLatch wait = new CountDownLatch(1);
final AtomicBoolean stopped = new AtomicBoolean();
Task[] tasks = new Task[threadCount];
final int[] getCounts = new int[threadCount];
final int offset = 1000000;
for (int i = 0; i < 100; i++) {
test.put(offset + i, i);
}
final int[] keys = new int[1000];
Random random = new Random(1);
for (int i = 0; i < keys.length; i++) {
int key;
do {
key = (int) Math.abs(random.nextGaussian() * 50);
} while (key > 100);
keys[i] = key;
}
for (int i = 0; i < threadCount; i++) {
final int x = i;
Task t = new Task() {
@Override
public void call() throws Exception {
Random random = new Random(x);
wait.await();
int i = 0;
for (; !stopped.get(); i++) {
int key = keys[random.nextInt(keys.length)];
test.get(offset + key);
if ((i & 127) == 0) {
test.put(offset + random.nextInt(100), random.nextInt());
}
}
getCounts[x] = i;
}
};
t.execute("t" + i);
tasks[i] = t;
}
wait.countDown();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
stopped.set(true);
for (Task t : tasks) {
t.get();
}
int totalCount = 0;
for (int x : getCounts) {
totalCount += x;
}
trace("requests: " + totalCount);
}
use of org.h2.util.Task in project h2database by h2database.
the class TestTriggersConstraints method testTriggerDeadlock.
private void testTriggerDeadlock() throws Exception {
final Connection conn, conn2;
final Statement stat, stat2;
conn = getConnection("trigger");
conn2 = getConnection("trigger");
stat = conn.createStatement();
stat2 = conn2.createStatement();
stat.execute("create table test(id int) as select 1");
stat.execute("create table test2(id int) as select 1");
stat.execute("create trigger test_u before update on test2 " + "for each row call \"" + DeleteTrigger.class.getName() + "\"");
conn.setAutoCommit(false);
conn2.setAutoCommit(false);
stat2.execute("update test set id = 2");
Task task = new Task() {
@Override
public void call() throws Exception {
Thread.sleep(300);
stat2.execute("update test2 set id = 4");
}
};
task.execute();
Thread.sleep(100);
try {
stat.execute("update test2 set id = 3");
task.get();
} catch (SQLException e) {
assertEquals(ErrorCode.LOCK_TIMEOUT_1, e.getErrorCode());
}
conn2.rollback();
conn.rollback();
stat.execute("drop table test");
stat.execute("drop table test2");
conn.close();
conn2.close();
}
Aggregations