Skip to content

Instantly share code, notes, and snippets.

@antonarhipov
Created February 12, 2014 21:31
Show Gist options
  • Save antonarhipov/8964958 to your computer and use it in GitHub Desktop.
Save antonarhipov/8964958 to your computer and use it in GitHub Desktop.
import java.util.BitSet;
import java.util.concurrent.CountDownLatch;
public class WordTearingExample {
public static void main(String[] args) throws Exception {
BitSet bs = new BitSet();
CountDownLatch latch = new CountDownLatch(1);
System.out.println("getting ready");
Thread t1 = new Thread(new Runnable() {
public void run() {
try {
latch.await();
Thread.sleep(1000);
} catch (Exception ex) {
}
bs.set(1);
System.out.println("thread1 done");
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
try {
latch.await();
Thread.sleep(1000);
} catch (Exception e) {
}
bs.set(2);
System.out.println("thread2 done");
}
});
t1.start();
t2.start();
latch.countDown();
t1.join();
t2.join();
System.out.println(bs.get(1));
System.out.println(bs.get(2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment