温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何正确的在java中使用同步锁

发布时间:2020-12-02 17:01:58 来源:亿速云 阅读:144 作者:Leah 栏目:编程语言

这篇文章给大家介绍如何正确的在java中使用同步锁,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

同步锁分类

对象锁(this)

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

字符串锁(比较特别)

应用场景

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

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

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

以上三类同步锁都可以。

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个线程会串行执行同步锁里面的代码。

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

错误用法

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个线程会同时执行同步锁里面的代码。

正确用法

方式一:

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个线程会串行执行同步锁里面的代码。

方式二:

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中使用同步锁就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI