synchronized与lock的区别

2年前 (2022) 程序员胖胖胖虎阿
216 0 0

synchronized和Lock都可以控制线程的同步,同一时间只能有一个线程操作上锁资源。

Lock是一个接口,而synchronized是Java中的关键字。

1.Lock是显式锁,可以手动开启和关闭,切结必须手动关闭,不然容易造成线程死锁。synchronized是隐式锁,出了作用域自动释放。

2.Lock只有代码块锁,而synchronized既有代码块锁,也有方法锁。

3.Lock锁性能更好,JVM花费少量的时间来调度线程。

未上锁资源

synchronized与lock的区别
synchronized与lock的区别

 1 public class TestLock {
 2     public static void main(String[] args) {
 3         Person2 person2 = new Person2();
 4         new Thread(person2,"可恶的黄牛").start();
 5         new Thread(person2,"苦逼的她们").start();
 6         new Thread(person2,"苦逼的我").start();
 7     }
 8 }
 9 class Person2 implements Runnable{
10     //定义lock锁
11     private ReentrantLock lock = new ReentrantLock();
12     int ticketNums = 10;
13     private boolean flag = true;
14     @Override
15     public  void run() {
16         while (flag){
17             try {
18                 Thread.sleep(100);
19                 if(ticketNums > 0){
20                     System.out.println(Thread.currentThread().getName()+"买到第"+ticketNums--+"张票");
21                 } else {
22                     flag = false;
23                 }
24             } catch (InterruptedException e) {
25                 e.printStackTrace();
26             }finally {
27             }
28         }
29     }
30 }

View Code

synchronized与lock的区别

使用synchronized关键字

 1 public class TestSysc {
 2     public static void main(String[] args) {
 3         Person person = new Person();
 4         new Thread(person,"可恶的黄牛").start();
 5         new Thread(person,"苦逼的她们").start();
 6         new Thread(person,"苦逼的我").start();
 7     }
 8 }
 9 class Person implements Runnable{
10     int ticketNums = 10;
11     private boolean flag = true;
12     @Override
13     public synchronized void run() {
14         while (flag){
15             if(ticketNums > 0){
16                 try {
17                     Thread.sleep(1000);
18                 } catch (InterruptedException e) {
19                     e.printStackTrace();
20                 }
21                 System.out.println(Thread.currentThread().getName()+"买到第"+ticketNums--+"张票");
22             }else {
23                 flag = false;
24             }
25         }
26     }
27 }

synchronized与lock的区别

 

 

 

使用Lock

 1 public class TestLock {
 2     public static void main(String[] args) {
 3         Person2 person2 = new Person2();
 4         new Thread(person2,"可恶的黄牛").start();
 5         new Thread(person2,"苦逼的她们").start();
 6         new Thread(person2,"苦逼的我").start();
 7     }
 8 }
 9 class Person2 implements Runnable{
10     //定义lock锁
11     private ReentrantLock lock = new ReentrantLock();
12     int ticketNums = 10;
13     private boolean flag = true;
14     @Override
15     public  void run() {
16         while (flag){
17             try {
18                 lock.lock();
19                 Thread.sleep(100);
20                 if(ticketNums > 0){
21                     System.out.println(Thread.currentThread().getName()+"买到第"+ticketNums--+"张票");
22                 } else {
23                     flag = false;
24                 }
25             } catch (InterruptedException e) {
26                 e.printStackTrace();
27             }finally {
28                 lock.unlock();
29             }
30         }
31     }
32 }

synchronized与lock的区别

 

 

版权声明:程序员胖胖胖虎阿 发表于 2022年9月18日 下午2:40。
转载请注明:synchronized与lock的区别 | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...