Synchronized的两个用法

深入理解Synchronized

Synchronized的作用:

能够保证在同一时刻最多只有一个线程执行该段代码,以达到保证并发安全的效果

Synchronized的两个用法:

1)对象锁

包括方法锁(默认锁对象为this当前实例对象)和同步代码块锁(自己指定锁对 象)

2)类锁:

Synchronized修饰静态的方法或指定锁为Class对象

 

 1 public class SynchronizedObjectCodeBlock2 implements Runnable {
 2 
 3     static SynchronizedObjectCodeBlock2 intface = new SynchronizedObjectCodeBlock2();
 4     Object lock1 = new Object();
 5     Object lock2 = new Object();
 6 
 7     @Override
 8     public void run() {
 9         synchronized (lock1) {
10             System.out.println("lock1部分。我叫:" + Thread.currentThread().getName());
11             try {
12                 Thread.sleep(3000);
13             } catch (InterruptedException e) {
14                 e.printStackTrace();
15             }
16             System.out.println(Thread.currentThread().getName() + "lock1部分运行结束!");
17         }
18 
19         synchronized (lock2) {
20             System.out.println("lock2部分。我叫:" + Thread.currentThread().getName());
21             try {
22                 Thread.sleep(3000);
23             } catch (InterruptedException e) {
24                 e.printStackTrace();
25             }
26             System.out.println(Thread.currentThread().getName() + "lock2部分运行结束!");
27         }
28     }
29 
30     public static void main(String[] args) {
31         Thread t1 = new Thread(intface);
32         Thread t2 = new Thread(intface);
33         t1.start();
34         t2.start();
35         while (t1.isAlive() || t2.isAlive()) {
36 
37         }
38         System.out.println("finshed");
39     }
40 }
同步代码块锁

 

运行结果:

 

 1 public class SynchronizedObjectMethod3 implements Runnable {
 2 
 3     static SynchronizedObjectMethod3 intface = new SynchronizedObjectMethod3();
 4 
 5     @Override
 6     public void run() {
 7         method();
 8     }
 9 
10     public static void main(String[] args) {
11         Thread t1 = new Thread(intface);
12         Thread t2 = new Thread(intface);
13         t1.start();
14         t2.start();
15         while (t1.isAlive() || t2.isAlive()) {
16 
17         }
18         System.out.println("finshed");
19     }
20 
21     public synchronized void method() {
22         System.out.println("我是对象锁的方法修饰符形式。我叫:"+ Thread.currentThread().getName());
23         try {
24             Thread.sleep(3000);
25         } catch (InterruptedException e) {
26             e.printStackTrace();
27         }
28         System.out.println(Thread.currentThread().getName()+"运行结束");
29     }
30 }
方法锁

 

运行结果:

 

 1 public class SynchronizedClassStatic4 implements Runnable {
 2 
 3     static SynchronizedClassStatic4 intface1 = new SynchronizedClassStatic4();
 4     static SynchronizedClassStatic4 intface2 = new SynchronizedClassStatic4();
 5 
 6     @Override
 7     public void run() {
 8         method();
 9     }
10 
11     public static synchronized void method(){
12         System.out.println("我是类锁的第一种形式:static形式。我叫:"+ Thread.currentThread().getName());
13         try {
14             Thread.sleep(3000);
15         } catch (InterruptedException e) {
16             e.printStackTrace();
17         }
18         System.out.println(Thread.currentThread().getName()+"运行结束");
19     }
20 
21     public static void main(String[] args) {
22         Thread t1 = new Thread(intface1);
23         Thread t2 = new Thread(intface2);
24         t1.start();
25         t2.start();
26         while (t1.isAlive() || t2.isAlive()) {
27 
28         }
29         System.out.println("finshed");
30     }
31 }
类锁的第一种形式:static形式

 

运行结果:

 1 public class SynchronizedClassClass5 implements Runnable {
 2 
 3     static SynchronizedClassClass5 intface1 = new SynchronizedClassClass5();
 4     static SynchronizedClassClass5 intface2 = new SynchronizedClassClass5();
 5 
 6     @Override
 7     public void run() {
 8         method();
 9     }
10 
11     public void method(){
12         synchronized (SynchronizedClassClass5.class){
13             System.out.println("我是类锁的第2种形式:synchronized (*.class)形式。我叫:"+ Thread.currentThread().getName());
14             try {
15                 Thread.sleep(3000);
16             } catch (InterruptedException e) {
17                 e.printStackTrace();
18             }
19             System.out.println(Thread.currentThread().getName()+"运行结束");
20         }
21     }
22 
23     public static void main(String[] args) {
24         Thread t1 = new Thread(intface1);
25         Thread t2 = new Thread(intface2);
26         t1.start();
27         t2.start();
28         while (t1.isAlive() || t2.isAlive()) {
29 
30         }
31         System.out.println("finshed");
32     }
33 }
类锁的第2种形式:synchronized (*.class)形式

运行结果:

转载于:https://www.cnblogs.com/nutural/p/10321789.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值