文章目录
- 1、公平锁、非公平锁
- 2、可重入锁
- 3、自旋锁
- 4、死锁
1、公平锁、非公平锁
公平锁:非常公平、不能插队、必须先来后到
- 非公平锁:非常不公平、可以插队、(默认非公平)
可以修改为公平锁
2、可重入锁
拿到外边的锁、就会自动拿到里边的锁。
package com.lock;
/**
* synchronized
*/
public class Demo1 {
public static void main(String[] args) {
Phone phone = new Phone();
new Thread(()->{
phone.sms();
},"A").start();
new Thread(()->{
phone.sms();
},"B").start();
}
}
class Phone{
public synchronized void sms(){
System.out.println(Thread.currentThread().getName()+"sms");
call();
}
public synchronized void call(){
System.out.println(Thread.currentThread().getName()+"call");
}
}
使用lock锁
package com.lock;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* synchronized
*/
public class Demo1 {
public static void main(String[] args) {
Phone phone = new Phone();
new Thread(()->{
phone.sms();
},"A").start();
new Thread(()->{
phone.sms();
},"B").start();
}
}
class Phone{
Lock lock = new ReentrantLock();
public void sms(){
lock.lock();
try {
System.out.println(Thread.currentThread().getName()+"sms");
call();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void call(){
lock.lock();
try {
System.out.println(Thread.currentThread().getName()+"call");
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
3、自旋锁
spinlock
自己写自旋锁
package com.lock;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicStampedReference;
public class SpinLockDemo {
AtomicReference<Thread> atomicReference = new AtomicReference<>();
//加锁
public void mylock(){
Thread thread = Thread.currentThread();
System.out.println(Thread.currentThread().getName()+"mylock");
while (!atomicReference.compareAndSet(null,thread)){
}
}
//解锁
public void myunlock(){
Thread thread = Thread.currentThread();
System.out.println(Thread.currentThread().getName()+"myunlock");
atomicReference.compareAndSet(thread,null);
}
}
package com.lock;
import java.util.concurrent.TimeUnit;
public class Test {
public static void main(String[] args) {
SpinLockDemo spinLockDemo = new SpinLockDemo();
new Thread(()->{
spinLockDemo.mylock();
try {
TimeUnit.SECONDS.sleep(3);
} catch (Exception e) {
e.printStackTrace();
} finally {
spinLockDemo.myunlock();
}
},"A").start();
new Thread(()->{
spinLockDemo.mylock();
try {
System.out.println();
} catch (Exception e) {
e.printStackTrace();
} finally {
spinLockDemo.myunlock();
}
},"B").start();
}
}
4、死锁
package com.lock;
import java.util.concurrent.TimeUnit;
public class DeadLockDemo {
public static void main(String[] args) {
String lockA="lockA";
String lockB ="lockB";
new Thread(new MyThread(lockA,lockB),"T1").start();
new Thread(new MyThread(lockB,lockA),"T2").start();
}
}
class MyThread implements Runnable{
private String lockA;
private String lockB;
public MyThread(String lockA,String lockB){
this.lockA = lockA;
this.lockB =lockB;
}
@Override
public void run() {
synchronized (lockA){
System.out.println(Thread.currentThread().getName()+"lock:"+lockA+"==>"+lockB);
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lockB){
System.out.println(Thread.currentThread().getName()+"lock:"+lockB+"==>"+lockA);
}
}
}
}
测试
1、使用jps定位进程号jps -l
2、使用jstack 进程号