首先谈谈 lock和Synchronized的区别:
synchronized是Java的一个关键字,也就是Java语言内置的特性,如果一个代码块被synchronized修饰了,当一个线程获取了对应的锁,执行代码块时,其他线程便只能一直等待,等待获取锁的线程释放锁,而获取锁的线程释放锁会有三种情况:
1).获取锁的线程执行完该代码块,然后线程释放对锁的占有;
2).线程执行发生异常,此时JVM会让线程自动释放锁;
3).调用wait方法,在等待的时候立即释放锁,方便其他的线程使用锁.
Lock不是Java语言内置的
1).synchronized是在JVM层面上实现的,如果代码执行出现异常,JVM会自动释放锁,但是Lock不行,要保证锁一定会被释放,就必须将unLock放到finally{}中(手动释放);
2).在资源竞争不是很激烈的情况下,Synchronized的性能要优于ReetarntLock,但是在很激烈的情况下,synchronized的性能会下降几十倍;
lock锁分为两种锁,ReentrantLock 和 ReadWriteLock。
ReentrantLock同步锁声明:
Lock lock = new ReentrantLock();
例:线程打印 1 2 3 4 ,线程一打印13(奇数),线程二打印24(偶数)
synchronized方式实现方式:
public class Demo5 {
public static void main(String[] args) {
Object object = new Object();
int[] count = {1};
Thread thread1 = new Thread(() -> {
for(int i = 0;i<2;i++){
synchronized(object){
while (count[0]%2 ==0){
try {
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("thread1:"+count[0]);
count[0]++;
object.notify();//线程唤醒,随机唤醒,哪个先抢到了cpu资源谁唤醒
}
}
});
Thread thread2 = new Thread(() -> {
for(int i = 0;i<2;i++){
synchronized(object){
while (count[0]%2 !=0){
try {
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("thread2:"+count[0]);
count[0]++;
object.notify();
}
}
});
thread1.start();
thread2.start();
}
}
运行结果:
Connected to the target VM, address: '127.0.0.1:57332', transport: 'socket'
thread1:1
thread2:2
thread1:3
thread2:4
Disconnected from the target VM, address: '127.0.0.1:57332', transport: 'socket'
Lock实现方式:
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Demo1 {
public static void main(String[] args) {
int[] count = {1};
Lock lock = new ReentrantLock();
Thread thread1 = new Thread(() -> {
for(int i = 0;i<2;){
try {
lock.lock();
while (count[0]%2 != 0){
System.out.println("thread1:"+count[0]);
count[0]++;
i++;
}
}finally {
lock.unlock();
}
}
});
Thread thread2 = new Thread(() -> {
for(int i = 0;i<2;){
try {
lock.lock();
while (count[0]%2 == 0){
System.out.println("thread2:"+count[0]);
count[0]++;
i++;
}
}finally {
lock.unlock();
}
}
});
thread1.start();
thread2.start();
}
}
上面已经说过lock不能自动释放线程锁,所以。
一定一定要记得手动释放线程。
运行结果:
thread1:1
thread2:2
thread1:3
thread2:4