死锁
死锁是指两个或两个以上的进程在执行过程中,由于竞争资源或者由于彼此通信而造成的一种阻塞的现象,若无外力作用,它们都将无法推进下去。此时称系统处于死锁状态或系统产生了死锁,这些永远在互相等待的进程称为死锁进程。
package com.smile.test.thread;
public class DeadLock {
public static void main(String[] args) {
MakeUp mk1 = new MakeUp(0, "小红");
MakeUp mk2 = new MakeUp(1, "小黑");
new Thread(mk1).start();
new Thread(mk2).start();
}
}
// 镜子对象
class Mirror{
}
// 口红对象
class LipStick{
}
// 化妆
class MakeUp implements Runnable{
static Mirror mirror = new Mirror();
static LipStick lipstick = new LipStick();
int choice; // 第一次选择的
String userName; // 化妆的人
// 有参构造
public MakeUp(int choice, String userName) {
this.choice = choice;
this.userName = userName;
}
@Override
public void run() {
try {
makeup();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void makeup() throws InterruptedException {
if (choice == 0) {
synchronized (mirror){
System.out.println(this.userName + "获得了Mirror");
Thread.sleep(1000);
synchronized(lipstick){
System.out.println(this.userName + "获得了LipStick");
}
}
}else {
synchronized (lipstick){
System.out.println(this.userName + "获得了LipStick");
Thread.sleep(1000);
synchronized(mirror){
System.out.println(this.userName + "获得了Mirror");
}
}
}
/*
不会引起死锁
private void makeup() throws InterruptedException {
if (choice == 0) {
synchronized (mirror){
System.out.println(this.userName + "获得了Mirror");
Thread.sleep(1000);
}
synchronized(lipstick){
System.out.println(this.userName + "获得了LipStick");
}
}else {
synchronized (lipstick){
System.out.println(this.userName + "获得了LipStick");
Thread.sleep(1000);
}
synchronized(mirror){
System.out.println(this.userName + "获得了Mirror");
}
}
}
*/
}
}
虽然进程在运行过程中,可能发生死锁,但死锁的发生也必须具备一定的条件,死锁的发生必须具备以下四个必要条件。
互斥条件:指进程对所分配到的资源进行排它性使用,即在一段时间内某资源只由一个进程占用。如果此时还有其它进程请求资源,则请求者只能等待,直至占有资源的进程用毕释放。
请求和保持条件:指进程已经保持至少一个资源,但又提出了新的资源请求,而该资源已被其它进程占有,此时请求进程阻塞,但又对自己已获得的其它资源保持不放。
不剥夺条件:指进程已获得的资源,在未使用完之前,不能被剥夺,只能在使用完时由自己释放。
环路等待条件:指在发生死锁时,必然存在一个进程——资源的环形链,即进程集合{P0,P1,P2,···,Pn}中的P0正在等待一个P1占用的资源;P1正在等待P2占用的资源,……,Pn正在等待已被P0占用的资源。
解决死锁:
Lock锁
package com.smile.test.thread.unsafethread;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
public class UnSafeArrayList {
public static void main(String[] args) throws InterruptedException {
List<String> list = new ArrayList<String>();
ReentrantLock lock = new ReentrantLock();
for (int i = 0; i < 5000; i++) {
new Thread(()->{
lock.lock();
list.add(Thread.currentThread().getName());
lock.unlock();
}).start();
}
Thread.sleep(3000);
System.out.println(list.size());
}
}
生产者消费者问题
管程法
package com.smile.test.thread;
/**
* 管程法
* @author 62741
*/
public class testPC {
public static void main(String[] args) {
SynContainer synContainer = new SynContainer();
new Product(synContainer).start();
new Consumer(synContainer).start();
}
}
// 生产者
class Product extends Thread{
SynContainer synContainer;
public Product(SynContainer synContainer){
this.synContainer = synContainer;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
synContainer.push(new Chicken(i));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Product" + i + "Chicken");
}
}
}
// 消费者
class Consumer extends Thread{
SynContainer synContainer;
public Consumer(SynContainer synContainer){
this.synContainer = synContainer;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
synContainer.pop();
System.out.println("Consumer" + synContainer.pop().id + "Chicken");
}
}
}
class Chicken{
int id;
public Chicken(int id) {
this.id = id;
}
}
class SynContainer{
Chicken[] chickens = new Chicken[10];
int count = 0;
public synchronized void push(Chicken chicken) throws InterruptedException {
if (count == chickens.length){
// 容器满了,通知消费者消费,生产者等待
this.wait();
}
chickens[count] = chicken;
count++ ;
this.notifyAll();
}
public synchronized Chicken pop(){
if (count == 0){
// 容器空,通知生产者生产,消费者等待
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count--;
Chicken chicken = chickens[count];
this.notifyAll();
return chicken;
}
}
信号灯法
package com.smile.test.thread;
public class SingnalMothod {
public static void main(String[] args) {
Play play = new Play();
new Actor(play).start();
new Watcher(play).start();
}
}
class Actor extends Thread{
Play play;
public Actor(Play play){
this.play = play;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
if (i%2 == 0){
play.play("第" + i + "集");
} else {
play.play("广告播放中...");
}
}
}
}
class Watcher extends Thread{
Play play;
public Watcher(Play play){
this.play = play;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
play.watch();
}
}
}
class Play{
String program;
boolean flag = true;
public synchronized void play(String program){
if(!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Actor表演了" + program);
this.notifyAll();
this.program = program;
this.flag = !this.flag;
}
public synchronized void watch(){
if(flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Watcher看了" + program);
this.notifyAll();
this.flag = !this.flag;
}
}