Skip to content

Instantly share code, notes, and snippets.

@c4pt0r
Created July 28, 2015 03:49
Show Gist options
  • Save c4pt0r/9f8a88e3a39532593e83 to your computer and use it in GitHub Desktop.
Save c4pt0r/9f8a88e3a39532593e83 to your computer and use it in GitHub Desktop.
while(true) {
Transaction transaction = new Transaction(conf, connection);
ThemisPut put = new ThemisPut(JOE).add(FAMILY, CASH, Bytes.toBytes(0));
transaction.put(CASHTABLE, put);
transaction.commit();
final Boolean[] b = {false};
Thread thread1 = new Thread() {
public void run() {
Transaction transaction = null;
try {
transaction = new Transaction(conf, connection);
ThemisGet get = new ThemisGet(JOE).addColumn(FAMILY, CASH);
int cash = Bytes.toInt(transaction.get(CASHTABLE, get).getValue(FAMILY, CASH));
if (cash == 0) {
ThemisPut put = new ThemisPut(JOE).add(FAMILY, CASH, Bytes.toBytes(1));
transaction.put(CASHTABLE, put);
transaction.commit();
System.out.println("t1" + " " + "success");
b[0] = false;
} else {
System.out.println("t1" + " " + cash);
}
} catch (IOException e) {
// on conflict
b[0] = true;
}
}
};
thread1.start();
Thread thread2 = new Thread() {
public void run() {
Transaction transaction = null;
try {
transaction = new Transaction(conf, connection);
ThemisGet get = new ThemisGet(JOE).addColumn(FAMILY, CASH);
int cash = Bytes.toInt(transaction.get(CASHTABLE, get).getValue(FAMILY, CASH));
if (cash == 0) {
ThemisPut put = new ThemisPut(JOE).add(FAMILY, CASH, Bytes.toBytes(2));
transaction.put(CASHTABLE, put);
transaction.commit();
System.out.println("t2" + " " + "success");
b[0] = false;
} else {
System.out.println("t2" + " " + cash);
}
} catch (IOException e) {
// on conflict
b[0] =true;
}
}
};
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (b[0] == false) {
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment