当前位置: 移动技术网 > IT编程>开发语言>Java > java同步锁的正确使用方法(必看篇)

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

同步锁分类

对象锁(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同步锁的正确使用方法(必看篇)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网