死锁
当使用static确定共享且只有一份资源,并且嵌套在同一个锁中,此时等待1秒后,仍然没有释放锁,然后申请获取另外一个的锁,此时产生死锁;
// 死锁:多个线程相互用着对方需要的资源,然后形成僵持
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; // 使用化妆品的人
@Override
public void run() {
// 化妆
try {
makeup();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Makeup(int choice, String girlName){
this.choice = choice;
this.girlName = girlName;
}
// 化妆, 互相持有对方的锁(资源)
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+"获得镜子的锁");
}
}
}
}
}
当把锁分开,等待1秒后释放锁,再获取另一样,就不造成死锁了
// 死锁:多个线程相互用着对方需要的资源,然后形成僵持
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; // 使用化妆品的人
@Override
public void run() {
// 化妆
try {
makeup();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Makeup(int choice, String girlName){
this.choice = choice;
this.girlName = girlName;
}
// 化妆, 互相持有对方的锁(资源)
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+"获得镜子的锁");
}
}
}
}
除了synchronized之外,JUC编程中还有显式定义同步锁来实现同步 java.util.concurrent.locks.Lock接口,锁提供了对共享资源的独占访问,每次只有一个线程对Lock对象加锁,线程开始访问共享资源前应先获得Lock对象;
可重入锁ReentrantLock类实现了Lock,拥有相同synchronized相同的并发性
public class TestLock {
public static void main(String[] args) {
AddLock addLock = new AddLock();
new Thread(addLock,"我").start();
new Thread(addLock,"你").start();
new Thread(addLock, "他").start();
}
}
class AddLock implements Runnable{
int ticketNum = 10;
// 定义lock锁 final常量 并使用try catch
private final ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
while(true){
try {
lock.lock();
if(ticketNum > 0){
System.out.println(Thread.currentThread().getName()+ticketNum--);
}else{
break;
}
}finally {
// 解锁
lock.unlock();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
在try中上锁,在finally中关锁;sleep不释放锁,所以不能放在lock与unlock之间
Lock与synchronized的对比
(1)Lock需要手动启动和关闭(显式锁),synchronized出了作用域自动释放(隐式锁)
(2)Lock只能代码块锁,synchronized技能代码块锁,也能方法锁
(3)Lock锁性能更好