死锁
Case1
package threadLearn.deadlock;
//死锁:多个线程
public class DeadLock {
public static void main(String[] args) {
Makeup g1 = new Makeup(0,"灰姑娘");
Makeup g2 = new Makeup(1,"白雪公主");
g1.start();
g2.start();
}
}
//口红
class Lipstick{
}
//镜子
class Mirror{
}
class Makeup extends Thread{
//需要的资源只有一份,用static来保证只有一份
static Lipstick lipstick=new Lipstick();
static Mirror mirror=new Mirror();
int choice;//选择
String girlName;//使用化妆品的人
Makeup(int choice,String girlName){
this.choice=choice;
this.girlName=girlName;
}
@Override
public void run() {
//化妆
try {
makeup();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//互相持有对方的锁,就是需要拿到对方的资源
private void makeup() throws InterruptedException {
if (choice==0){
synchronized (lipstick){//获得口红的锁
System.out.println(this.girlName+" 获得口红的锁");
Thread.sleep(3000);
synchronized (mirror){//一秒钟后想获得镜子
System.out.println(this.girlName+" 获得镜子的锁");
}
}
}else{
synchronized (mirror){
System.out.println(this.girlName+" 获得镜子的锁");
Thread.sleep(2000);
synchronized (lipstick){
System.out.println(this.girlName+" 获得口红的锁");
}
}
}
}
}
解决方案:
package threadLearn.deadlock;
//死锁:多个线程
public class DeadLock {
public static void main(String[] args) {
Makeup g1 = new Makeup(0,"灰姑娘");
Makeup g2 = new Makeup(1,"白雪公主");
g1.start();
g2.start();
}
}
//口红
class Lipstick{
}
//镜子
class Mirror{
}
class Makeup extends Thread{
//需要的资源只有一份,用static来保证只有一份
static Lipstick lipstick=new Lipstick();
static Mirror mirror=new Mirror();
int choice;//选择
String girlName;//使用化妆品的人
Makeup(int choice,String girlName){
this.choice=choice;
this.girlName=girlName;
}
@Override
public void run() {
//化妆
try {
makeup();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//互相持有对方的锁,就是需要拿到对方的资源
private void makeup() throws InterruptedException {
if (choice==0){
synchronized (lipstick){//获得口红的锁
System.out.println(this.girlName+" 获得口红的锁");
Thread.sleep(1000);
}
synchronized (mirror){//一秒钟后想获得镜子
System.out.println(this.girlName+" 获得镜子的锁");
}
}else{
synchronized (mirror){
System.out.println(this.girlName+" 获得镜子的锁");
Thread.sleep(2000);
}
synchronized (lipstick){
System.out.println(this.girlName+" 获得口红的锁");
}
}
}
}
Lock锁
synchronized是隐式锁,Lock是显示锁
举例:
package threadLearn.deadlock;
//测试Lock锁
public class testLock {
public static void main(String[] args) {
testLock2 testLock2 = new testLock2();
new Thread(testLock2).start();
new Thread(testLock2).start();
new Thread(testLock2).start();
}
}
class testLock2 implements Runnable{
int ticketNums=10;
@Override
public void run() {
while (true){
if (ticketNums>0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(ticketNums--);
}else{
break;
}
}
}
}
输出:
9
8
10
7
7
7
6
6
5
4
2
3
1
-1
0
解决
package threadLearn.deadlock;
import java.util.concurrent.locks.ReentrantLock;
//测试Lock锁
public class testLock {
public static void main(String[] args) {
testLock2 testLock2 = new testLock2();
new Thread(testLock2).start();
new Thread(testLock2).start();
new Thread(testLock2).start();
}
}
class testLock2 implements Runnable{
int ticketNums=10;
//定义lock锁
private final ReentrantLock lock =new ReentrantLock();
@Override
public void run() {
while (true){
try {
lock.lock();//加锁
if (ticketNums>0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(ticketNums--);
}else{
break;
}
}finally {
lock.unlock();//解锁
}
}
}
}
输出:
10
9
8
7
6
5
4
3
2
1
ReentrantLock可重用锁