public static void main(String[] args) { Random rand = new Random(); System.out.println("Initializing keeper"); try { ZooKeeper zk; zk = new ZooKeeper( "localhost:3661" // address , 3000 // timeout , null // Watcher: not actually need right now ); for (int i = 0; i < 10; ++i) { // NB!!! System.err.println("- Wanna do some staff..."); while (!lock(zk, LOCK_NAME)) { // NB!!! Thread.sleep(Math.abs(rand.nextLong()) % 500 + 500); // NB!!! } // NB!!! doSmth(); // NB!!! unlock(zk, LOCK_NAME); // NB!!! System.err.println("- Procrastinating..."); Thread.sleep(Math.abs(rand.nextLong()) % 500 + 700); // NB!!! } // NB!!! } catch (Exception e) { System.err.println("Exception while init: " + e); } }
public static void main(String[] args) { if (args.length < 1) { // NB!! System.err.println("x Usage: ./run connection_string"); // NB!! System.exit(1); // NB!! } // NB!! Random rand = new Random(); System.out.println("Initializing keeper"); Main remoteLock = new Main(); try { ZooKeeper zk; zk = new ZooKeeper(args[0], 3000, null); // NB!! for (int i = 0; i < 10; ++i) { System.err.println("- Wanna do some staff..."); String lockName = remoteLock.lock(zk); doSmth(); remoteLock.unlock(zk, lockName); System.err.println("- Procrastinating..."); Thread.sleep(Math.abs(rand.nextLong()) % 500 + 700); } } catch (Exception e) { System.err.println("Exception while init: " + e); } }