java 多线程 锁

来源:互联网 发布:首页源码 编辑:程序博客网 时间:2024/05/01 06:49

在Java多线程中,我们可以使用synchronized关键字来实现线程间的同步互斥工作,那么其实还有一个更优秀的机制去完成 这个同步互斥的工作他就是Lock对象


重入锁,在需要进行同步的代码部分加上锁定,但不要忘记最后一定要释放锁定,不然会造成永远无法释放,其他线程永远进不来的结果。

public class UseReentrantLock {private Lock lock = new ReentrantLock();public void method1(){try {lock.lock();System.out.println("当前线程:" + Thread.currentThread().getName() + "进入method1..");Thread.sleep(1000);System.out.println("当前线程:" + Thread.currentThread().getName() + "退出method1..");Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();} finally {lock.unlock();}}public void method2(){try {lock.lock();System.out.println("当前线程:" + Thread.currentThread().getName() + "进入method2..");Thread.sleep(2000);System.out.println("当前线程:" + Thread.currentThread().getName() + "退出method2..");Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();} finally {lock.unlock();}}public static void main(String[] args) {final UseReentrantLock ur = new UseReentrantLock();Thread t1 = new Thread(new Runnable() {@Overridepublic void run() {ur.method1();ur.method2();}}, "t1");t1.start();try {Thread.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}//System.out.println(ur.lock.getQueueLength());}}

当前线程:t1进入method1..当前线程:t1退出method1..当前线程:t1进入method2..当前线程:t1退出method2..

还记得我们在使用synchronized的时候,如果需要多线程间协作工作则需要Object 的wait()和notify方法进行配合工作

那么同样 我们在使用Lock的时候,可以使用一个新的等待/通知的类,它就是Condition,这个类一定是针对具体某一把锁的,也就是在只有锁的基础上才会产生Condition


public class UseCondition {private Lock lock = new ReentrantLock();private Condition condition = lock.newCondition();public void method1(){try {lock.lock();System.out.println("当前线程:" + Thread.currentThread().getName() + "进入等待状态..");Thread.sleep(3000);System.out.println("当前线程:" + Thread.currentThread().getName() + "释放锁..");condition.await();// Object waitSystem.out.println("当前线程:" + Thread.currentThread().getName() +"继续执行...");} catch (Exception e) {e.printStackTrace();} finally {lock.unlock();}}public void method2(){try {lock.lock();System.out.println("当前线程:" + Thread.currentThread().getName() + "进入..");Thread.sleep(3000);System.out.println("当前线程:" + Thread.currentThread().getName() + "发出唤醒..");condition.signal();//Object notify} catch (Exception e) {e.printStackTrace();} finally {lock.unlock();}}public static void main(String[] args) {final UseCondition uc = new UseCondition();Thread t1 = new Thread(new Runnable() {@Overridepublic void run() {uc.method1();}}, "t1");Thread t2 = new Thread(new Runnable() {@Overridepublic void run() {uc.method2();}}, "t2");t1.start();t2.start();}}

当前线程:t1进入等待状态..当前线程:t1释放锁..当前线程:t2进入..当前线程:t2发出唤醒..当前线程:t1继续执行...

多个Condtion

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("当前线程:" +Thread.currentThread().getName() + "进入方法m1等待..");c1.await();System.out.println("当前线程:" +Thread.currentThread().getName() + "方法m1继续..");} catch (Exception e) {e.printStackTrace();} finally {lock.unlock();}}public void m2(){try {lock.lock();System.out.println("当前线程:" +Thread.currentThread().getName() + "进入方法m2等待..");c1.await();System.out.println("当前线程:" +Thread.currentThread().getName() + "方法m2继续..");} catch (Exception e) {e.printStackTrace();} finally {lock.unlock();}}public void m3(){try {lock.lock();System.out.println("当前线程:" +Thread.currentThread().getName() + "进入方法m3等待..");c2.await();System.out.println("当前线程:" +Thread.currentThread().getName() + "方法m3继续..");} catch (Exception e) {e.printStackTrace();} finally {lock.unlock();}}public void m4(){try {lock.lock();System.out.println("当前线程:" +Thread.currentThread().getName() + "唤醒..");c1.signalAll();} catch (Exception e) {e.printStackTrace();} finally {lock.unlock();}}public void m5(){try {lock.lock();System.out.println("当前线程:" +Thread.currentThread().getName() + "唤醒..");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() {@Overridepublic void run() {umc.m1();}},"t1");Thread t2 = new Thread(new Runnable() {@Overridepublic void run() {umc.m2();}},"t2");Thread t3 = new Thread(new Runnable() {@Overridepublic void run() {umc.m3();}},"t3");Thread t4 = new Thread(new Runnable() {@Overridepublic void run() {umc.m4();}},"t4");Thread t5 = new Thread(new Runnable() {@Overridepublic void run() {umc.m5();}},"t5");t1.start();// c1t2.start();// c1t3.start();// c2try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}t4.start();// c1try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}t5.start();// c2}}


当前线程:t1进入方法m1等待..当前线程:t2进入方法m2等待..当前线程:t3进入方法m3等待..当前线程:t4唤醒..当前线程:t1方法m1继续..当前线程:t2方法m2继续..当前线程:t5唤醒..当前线程:t3方法m3继续..



0 0
原创粉丝点击