服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|

服务器之家 - 编程语言 - JAVA教程 - java同步锁的正确使用方法(必看篇)

java同步锁的正确使用方法(必看篇)

2020-12-20 14:24zhuwenqi2016 JAVA教程

下面小编就为大家带来一篇java同步锁的正确使用方法(必看篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

同步锁分类

对象锁(this)

类锁(类的字节码文件对象即类名.class)

字符串锁(比较特别)

应用场景

在多线程下对共享资源的安全操作。

需求:启动5个线程对共享资源total进行安全操作。

同步锁在多线程单例模式下的使用

以上三类同步锁都可以。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package cn.myThread;
 
public class MyThread implements Runnable {
 private static int total = 10;
 @Override
 public void run() {
  synchronized (this){ //使用this对象锁
  //synchronized (MyThread.class){ //使用MyThread.class类锁
  //synchronized (""){//使用字符串锁
   System.out.println(Thread.currentThread().getName() + "正在运行");
   try {
    Thread.sleep(100);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   total--;
   System.out.println(total);
   System.out.println(Thread.currentThread().getName() + "线程结束");
  }
 }
}
 
package cn.test;
 
import cn.myThread.MyThread;
 
public class TestMyThread {
 public static void main(String[] args){
  MyThread myThread = new MyThread();
  Thread thread = null;
  for (int i = 1 ; i <= 5 ; i++){
   thread = new Thread(myThread,"线程"+i); //开启5个线程,传入同一个对象
   thread.start();
  }
 }
}
 
线程1正在运行
 
9
 
线程1线程结束
 
线程3正在运行
 
8
 
线程3线程结束
 
线程5正在运行
 
7
 
线程5线程结束
 
线程2正在运行
 
6
 
线程2线程结束
 
线程4正在运行
 
5
 
线程4线程结束

分析:从运行结果可以看出5个线程串行执行同步锁里面的代码,因为5个线程中的同步锁对象this指向同一个的对象(同步锁对象MyThread.class类锁是同一个对象、同步锁对象 ”” 字符串锁是同一个对象),所以5个线程会串行执行同步锁里面的代码。

同步锁在多线程多例模式下的使用

错误用法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package cn.myThread;
 
public class MyThread implements Runnable {
 private static int total = 10;
 @Override
 public void run() {
  synchronized (this){//使用this对象锁
   System.out.println(Thread.currentThread().getName() + "正在运行");
   try {
    Thread.sleep(100);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   total--;
   System.out.println(total);
   System.out.println(Thread.currentThread().getName() + "线程结束");
  }
 }
}
 
package cn.test;
 
import cn.myThread.MyThread;
 
public class TestMyThread {
 public static void main(String[] args){
  Thread thread = null;
  for (int i = 1 ; i <= 5 ; i++){
   thread = new Thread(new MyThread(),"线程"+i);//开启5个线程,传入5个不同对象
   thread.start();
  }
 }
}
 
线程2正在运行
 
线程1正在运行
 
线程3正在运行
 
线程5正在运行
 
线程4正在运行
 
9
 
7
 
9
 
8
 
线程1线程结束
 
线程5线程结束
 
线程2线程结束
 
线程3线程结束
 
6
 
线程4线程结束

分析:从运行结果可以看出5个线程并行执行同步锁里面的代码,因为5个线程中的同步锁对象this指向5个不同的对象,所以5个线程会同时执行同步锁里面的代码。

正确用法

方式一:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package cn.myThread;
 
public class MyThread implements Runnable {
 private static int total = 10;
 @Override
 public void run() {
  synchronized (MyThread.class){//使用MyThread.class类锁
   System.out.println(Thread.currentThread().getName() + "正在运行");
   try {
    Thread.sleep(100);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   total--;
   System.out.println(total);
   System.out.println(Thread.currentThread().getName() + "线程结束");
  }
 }
}
 
package cn.test;
 
import cn.myThread.MyThread;
 
public class TestMyThread {
 public static void main(String[] args){
  Thread thread = null;
  for (int i = 1 ; i <= 5 ; i++){
   thread = new Thread(new MyThread(),"线程"+i); //开启5个线程,传入5个不同对象
   thread.start();
  }
 }
}
 
线程1正在运行
 
9
 
线程1线程结束
 
线程5正在运行
 
8
 
线程5线程结束
 
线程4正在运行
 
7
 
线程4线程结束
 
线程3正在运行
 
6
 
线程3线程结束
 
线程2正在运行
 
5
 
线程2线程结束

分析:从运行结果可以看出5个线程串行执行同步锁里面的代码,因为5个线程中的同步锁对象MyThread.class类锁是同一个对象,所以5个线程会串行执行同步锁里面的代码。

方式二:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package cn.myThread;
 
public class MyThread implements Runnable {
 private static int total = 10;
 @Override
 public void run() {
  synchronized (""){//使用字符串锁
   System.out.println(Thread.currentThread().getName() + "正在运行");
   try {
    Thread.sleep(100);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   total--;
   System.out.println(total);
   System.out.println(Thread.currentThread().getName() + "线程结束");
  }
 }
}
 
package cn.test;
 
import cn.myThread.MyThread;
 
public class TestMyThread {
 public static void main(String[] args){
  Thread thread = null;
  for (int i = 1 ; i <= 5 ; i++){
   thread = new Thread(new MyThread(),"线程"+i); //开启5个线程,传入5个不同对象
   thread.start();
  }
 }
}
 
线程1正在运行
 
9
 
线程1线程结束
 
线程4正在运行
 
8
 
线程4线程结束
 
线程5正在运行
 
7
 
线程5线程结束
 
线程3正在运行
 
6
 
线程3线程结束
 
线程2正在运行
 
5
 
线程2线程结束

分析:从运行结果可以看出5个线程串行执行同步锁里面的代码,因为5个线程中的同步锁对象 ”” 字符串锁是同一个对象,所以5个线程会串行执行同步锁里面的代码。

以上这篇java同步锁的正确使用方法(必看篇)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/zhuwenqi2016/p/7419356.html

延伸 · 阅读

精彩推荐