ReentrantLock() multiple Condition c1 = lock.newCondit() use

package com.bjsxt.height.lock020;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class UseManyCondition {

	private ReentrantLock lock = new ReentrantLock();
	private Condition c1 = lock.newCondition();
	private Condition c2 = lock.newCondition();
	
	public void m1(){
		try {
			lock.lock();
			System.out.println("Current thread:" +Thread.currentThread().getName() + "Enter method m1 to wait..");
			c1.await();
			System.out.println("Current thread:" +Thread.currentThread().getName() + "Method m1 continues..");
		} catch (Exception e) {
			e.printStackTrace ();
		} finally {
			lock.unlock();
		}
	}
	
	public void m2(){
		try {
			lock.lock();
			System.out.println("Current thread:" +Thread.currentThread().getName() + "Enter method m2 to wait..");
			c1.await();
			System.out.println("Current thread:" +Thread.currentThread().getName() + "Method m2 continues..");
		} catch (Exception e) {
			e.printStackTrace ();
		} finally {
			lock.unlock();
		}
	}
	
	public void m3(){
		try {
			lock.lock();
			System.out.println("Current thread: " +Thread.currentThread().getName() + "Enter method m3 to wait..");
			c2.await();
			System.out.println("Current thread:" +Thread.currentThread().getName() + "Method m3 continues..");
		} catch (Exception e) {
			e.printStackTrace ();
		} finally {
			lock.unlock();
		}
	}
	
	public void m4(){
		try {
			lock.lock();
			System.out.println("Current thread:" +Thread.currentThread().getName() + "Wake up..");
			c1.signalAll();
		} catch (Exception e) {
			e.printStackTrace ();
		} finally {
			lock.unlock();
		}
	}
	
	public void m5(){
		try {
			lock.lock();
			System.out.println("Current thread:" +Thread.currentThread().getName() + "Wake up..");
			c2.signal();
		} catch (Exception e) {
			e.printStackTrace ();
		} finally {
			lock.unlock();
		}
	}
	
	public static void main(String[] args) {
		
		
		final UseManyCondition umc = new UseManyCondition ();
		Thread t1 = new Thread(new Runnable() {
			@Override
			public void run() {
				umc.m1();
			}
		},"t1");
		Thread t2 = new Thread(new Runnable() {
			@Override
			public void run() {
				umc.m2();
			}
		},"t2");
		Thread t3 = new Thread(new Runnable() {
			@Override
			public void run() {
				umc.m3();
			}
		},"t3");
		Thread t4 = new Thread(new Runnable() {
			@Override
			public void run() {
				umc.m4();
			}
		},"t4");
		Thread t5 = new Thread(new Runnable() {
			@Override
			public void run() {
				umc.m5();
			}
		},"t5");
		
		t1.start();	// c1
		t2.start();	// c1
		t3.start();	// c2
		

		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace ();
		}

		t4.start();	// c1
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace ();
		}
		t5.start();	// c2
		
	}
	
	
	
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326115717&siteId=291194637