Skip to content

Instantly share code, notes, and snippets.

@finnkvan
Created October 24, 2018 19:29
Show Gist options
  • Select an option

  • Save finnkvan/0dbb9c4dffefe74942f6689324c24c64 to your computer and use it in GitHub Desktop.

Select an option

Save finnkvan/0dbb9c4dffefe74942f6689324c24c64 to your computer and use it in GitHub Desktop.
ReetranLook
package com.pivincii;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class AsynDemo {
Lock lock = new ReentrantLock();
public void countTo(int value) {
lock.lock();
try {
int count = 0;
while (count < value) {
Thread.sleep(1000);
count++;
System.out.println(Thread.currentThread() + " counted " + count);
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public static void main(String[] args) {
AsynDemo asynDemo = new AsynDemo();
new Thread(() -> asynDemo.countTo(3)).start();
new Thread(() -> asynDemo.countTo(3)).start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment