![img_c332e8500c2facc3af2195550ede044d.png](https://i-blog.csdnimg.cn/blog_migrate/17ec2d0d8cb581ac17db8511b18186bb.png)
![img_4582f193f8f91f87d471f9a4ffa385c9.png](https://i-blog.csdnimg.cn/blog_migrate/9f29e9e309e3428d10c912e6c84e4a8b.png)
![img_fcee47bd328d794a96cbf9eb9ccabb2d.png](https://i-blog.csdnimg.cn/blog_migrate/289a278594ad8a08bbd097f7a7c38cec.png)
![img_5dfb0c30695e335cb62b361863fe51b1.png](https://i-blog.csdnimg.cn/blog_migrate/235c4adfd46b063d3fc2845907fc018d.png)
![img_74c73ebf2a1f65edf7d0db37b8bf9bbb.png](https://i-blog.csdnimg.cn/blog_migrate/ec639ca9eb050a82d8c74a0316ced654.png)
![img_424138b428eb8270c448935a46a288b2.png](https://i-blog.csdnimg.cn/blog_migrate/bccec4f623e797ca1bb94527591ae693.png)
![img_f3b2d77c490187b18215d3408221ef41.png](https://i-blog.csdnimg.cn/blog_migrate/09c64c91ba449f6dd608386393032e6b.png)
锁的概念:
![img_8d6930444e419a10c66df7aef3d8c1ad.png](https://i-blog.csdnimg.cn/blog_migrate/6d2be19e9deae0a871dee7e6a6938f0b.png)
![img_f662642856451782b61f5300172c42ef.png](https://i-blog.csdnimg.cn/blog_migrate/93ff8f3e4e4778a8793f96940bb9175f.png)
package com.bjsxt.height.lock020;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
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() {
@Override
public void run() {
ur.method1();
}
}, "t1");
t1.start();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
ur.method1();
}
}, "t2");
t2.start();
//System.out.println(ur.lock.getQueueLength());
}
}
![img_cb40df21ddac590ba861c7a52b9f90c1.png](https://i-blog.csdnimg.cn/blog_migrate/631668022c7ff7b96aecb0a45e339f67.png)
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 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 wait
System.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() {
@Override
public void run() {
uc.method1();
}
}, "t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
uc.method2();
}
}, "t2");
t1.start();
t2.start();
}
}
![img_a8cc584a70edf9417d64c11ef933dad1.png](https://i-blog.csdnimg.cn/blog_migrate/6dd6f0bac7760bbb3fc55e7b84600947.png)
![img_6b638af4a111c2c9566d96118118ea1e.png](https://i-blog.csdnimg.cn/blog_migrate/7af43b9a1d348f6d73e45c5d0803666c.png)
![img_45d43896e2980ae1e11ee27a66641be0.png](https://i-blog.csdnimg.cn/blog_migrate/9458b1aa0509a93ea64f218b440f758a.png)