Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ThreadTest2 {
- Lock lock = new ReentrantLock();
- Condition condition = lock.newCondition();
- public void conditionWait() throws InterruptedException {
- lock.lock();
- try {
- System.out.println("1");
- condition.await();
- System.out.println("2");
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- lock.unlock();
- }
- }
- public void conditionSignal() throws InterruptedException {
- lock.lock();
- try {
- System.out.println("3");
- condition.signal();
- System.out.println("4");
- } finally {
- lock.unlock();
- }
- }
- public static void main(String[] args) throws InterruptedException {
- ThreadTest2 test = new ThreadTest2();
- test.conditionWait();
- Thread.sleep(2000);
- test.conditionSignal();
- }
- }
- public class ThreadTest2 {
- Lock lock = new ReentrantLock();
- Condition condition = lock.newCondition();
- public void conditionWait() throws InterruptedException {
- new Thread() {
- public void run() {
- lock.lock();
- try {
- System.out.println("1");
- condition.await();
- System.out.println("2");
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally {
- lock.unlock();
- }
- };
- }.start();
- }
- public void conditionSignal() throws InterruptedException{
- new Thread() {
- public void run() {
- lock.lock();
- try {
- System.out.println("3");
- condition.signal();
- System.out.println("4");
- } finally {
- lock.unlock();
- }
- };
- }.start();
- }
- public static void main(String[] args) throws InterruptedException {
- ThreadTest2 test = new ThreadTest2();
- test.conditionWait();
- Thread.sleep(2000);
- test.conditionSignal();
- }
- }
Add Comment
Please, Sign In to add comment