![img_c332e8500c2facc3af2195550ede044d.png](https://yqfile.alicdn.com/img_c332e8500c2facc3af2195550ede044d.png?x-oss-process=image/resize,w_1400/format,webp)
![img_4582f193f8f91f87d471f9a4ffa385c9.png](https://yqfile.alicdn.com/img_4582f193f8f91f87d471f9a4ffa385c9.png?x-oss-process=image/resize,w_1400/format,webp)
![img_fcee47bd328d794a96cbf9eb9ccabb2d.png](https://yqfile.alicdn.com/img_fcee47bd328d794a96cbf9eb9ccabb2d.png?x-oss-process=image/resize,w_1400/format,webp)
![img_5dfb0c30695e335cb62b361863fe51b1.png](https://yqfile.alicdn.com/img_5dfb0c30695e335cb62b361863fe51b1.png?x-oss-process=image/resize,w_1400/format,webp)
![img_74c73ebf2a1f65edf7d0db37b8bf9bbb.png](https://yqfile.alicdn.com/img_74c73ebf2a1f65edf7d0db37b8bf9bbb.png?x-oss-process=image/resize,w_1400/format,webp)
![img_424138b428eb8270c448935a46a288b2.png](https://yqfile.alicdn.com/img_424138b428eb8270c448935a46a288b2.png?x-oss-process=image/resize,w_1400/format,webp)
![img_f3b2d77c490187b18215d3408221ef41.png](https://yqfile.alicdn.com/img_f3b2d77c490187b18215d3408221ef41.png?x-oss-process=image/resize,w_1400/format,webp)
锁的概念:
![img_8d6930444e419a10c66df7aef3d8c1ad.png](https://yqfile.alicdn.com/img_8d6930444e419a10c66df7aef3d8c1ad.png?x-oss-process=image/resize,w_1400/format,webp)
![img_f662642856451782b61f5300172c42ef.png](https://yqfile.alicdn.com/img_f662642856451782b61f5300172c42ef.png?x-oss-process=image/resize,w_1400/format,webp)
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://yqfile.alicdn.com/img_cb40df21ddac590ba861c7a52b9f90c1.png?x-oss-process=image/resize,w_1400/format,webp)
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://yqfile.alicdn.com/img_a8cc584a70edf9417d64c11ef933dad1.png?x-oss-process=image/resize,w_1400/format,webp)
![img_6b638af4a111c2c9566d96118118ea1e.png](https://yqfile.alicdn.com/img_6b638af4a111c2c9566d96118118ea1e.png?x-oss-process=image/resize,w_1400/format,webp)
![img_45d43896e2980ae1e11ee27a66641be0.png](https://yqfile.alicdn.com/img_45d43896e2980ae1e11ee27a66641be0.png?x-oss-process=image/resize,w_1400/format,webp)