博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java synchronized
阅读量:6343 次
发布时间:2019-06-22

本文共 11078 字,大约阅读时间需要 36 分钟。

Synchronized是Java中解决并发问题的一种最常用的方法,也是最简单的一种方法。Synchronized的作用主要有三个:(1)确保线程互斥的访问同步代码(2)保证共享变量的修改能够及时可见(3)有效解决重排序问题。从语法上讲,Synchronized总共有三种用法:

  (1)修饰普通方法

  (2)修饰静态方法

  (3)修饰代码块   

synchronized形式(均写在在类A中) 锁类型
synchronized(obj){} obj 对象锁
synchronized(A.class){} A.class 类锁
synchronized(this){} 当前类对象 对象锁
synchronized void memberMethod(){} 当前类对象 对象锁
static synchronized void staticMethod(){} 当前类 类锁

在多线程开发中,我们经常看到synchronized(this)、synchronized(*.class)、synchronized(任意对象)这几种类型同步方法。但是是否知道这几种写法有什么区别了?下面根据代码来分析:

synchronized代码块间的同步性

package com.zwz.thread.demo1;public class ObjectService {	public void serviceMethodA(){		try {			synchronized (this) {				System.out.println("A begin time="+System.currentTimeMillis());				Thread.sleep(2000);				System.out.println("A end   time="+System.currentTimeMillis());			}		} catch (InterruptedException e) {			e.printStackTrace();		}	}	public void serviceMethodB(){		synchronized (this) {			System.out.println("B begin time="+System.currentTimeMillis());			System.out.println("B end   time="+System.currentTimeMillis());		}	}}复制代码
package com.zwz.thread.demo1;public class ThreadA extends Thread {	private ObjectService objectService;	public ThreadA(ObjectService objectService){		super();		this.objectService=objectService;	}	@Override	public void run() {		super.run();		objectService.serviceMethodA();	}}复制代码
package com.zwz.thread.demo1;public class ThreadB extends Thread {	private ObjectService objectService;	public ThreadB(ObjectService objectService){		super();		this.objectService=objectService;	}	@Override	public void run() {		super.run();		objectService.serviceMethodB();	}}复制代码
package com.zwz.thread.demo1;public class MainTest {	public static void main(String[] args) {		ObjectService service=new ObjectService();		ThreadA a=new ThreadA(service);		a.setName("a");		a.start();		ThreadB b=new ThreadB(service);		b.setName("b");		b.start();	}}复制代码

运行结果:

结论: 当一个线程访问ObjectService的一个synchronized (this)同步代码块时,其它线程对同一个ObjectService中其它的synchronized (this)同步代码块的访问将是堵塞,这说明synchronized (this)使用的对象监视器是一个。

验证synchronized (this)代码块是锁定当前对象

package com.zwz.thread.demo2;public class ObjectService {	public void objectMethodA(){		System.out.println("run----objectMethodA");	}	public void objectMethodB(){		synchronized (this) {			try {				for (int i = 1; i <= 10; i++) {					System.out.println("synchronized thread name:"+Thread.currentThread().getName()+"-->i="+i);					Thread.sleep(1000);				}			} catch (InterruptedException e) {					e.printStackTrace();			}		}	}}复制代码
package com.zwz.thread.demo2;public class ThreadA extends Thread {	private ObjectService objectService;	public ThreadA(ObjectService objectService) {		super();		this.objectService = objectService;	}	@Override	public void run() {		super.run();		objectService.objectMethodA();	}}复制代码
package com.zwz.thread.demo2;public class ThreadB extends Thread {	private ObjectService objectService;	public ThreadB(ObjectService objectService) {		super();		this.objectService = objectService;	}	@Override	public void run() {		super.run();		objectService.objectMethodB();	}}复制代码
package com.zwz.thread.demo2;public class MainTest {	public static void main(String[] args) throws InterruptedException {		ObjectService service=new ObjectService();		ThreadB b=new ThreadB(service);		b.start();		Thread.sleep(2000);		ThreadA a=new ThreadA(service);		a.start();	}}复制代码

运行结果:

可以看到objectMethodA方法异步执行了,下面我们将objectMethodA()加上同步。

package com.zwz.thread.demo2;public class ObjectService {	public synchronized void objectMethodA(){		System.out.println("run----objectMethodA");	}	public void objectMethodB(){		synchronized (this) {			try {				for (int i = 1; i <= 10; i++) {					System.out.println("synchronized thread name:"+Thread.currentThread().getName()+"-->i="+i);					Thread.sleep(1000);				}			} catch (InterruptedException e) {					e.printStackTrace();			}		}	}}复制代码

运行结果:

结论:

上面三个小例子我们可以知道,多个线程调用同一个对象中的不同名称的synchronized同步方法或synchronized(this)同步代码块时,是同步的。

1、synchronized同步方法

①对其它的synchronized同步方法或synchronized(this)同步代码块调用是堵塞状态;

②同一时间只有一个线程执行synchronized同步方法中的代码。

2、synchronized(this)同步代码块

①对其它的synchronized同步方法或synchronized(this)同步代码块调用是堵塞状态;

②同一时间只有一个线程执行synchronized同步方法中的代码。

将任意对象作为对象监视器

package com.zwz.thread.demo3;public class ObjectService {	private String uname;	private String pwd;	String lock=new String();	public void setUserNamePassWord(String userName,String passWord){		try {			synchronized (lock) {				System.out.println("thread name="+Thread.currentThread().getName()						+" 进入代码快:"+System.currentTimeMillis());				uname=userName;				Thread.sleep(3000);				pwd=passWord;				System.out.println("thread name="+Thread.currentThread().getName()						+" 进入代码快:"+System.currentTimeMillis()+"入参uname:"+uname+"入参pwd:"+pwd);			}		} catch (InterruptedException e) {			e.printStackTrace();		}	}}复制代码
package com.zwz.thread.demo3;public class ThreadA extends Thread {	private ObjectService objectService;	public ThreadA(ObjectService objectService) {		super();		this.objectService = objectService;	}	@Override	public void run() {		objectService.setUserNamePassWord("a", "aa");	}}复制代码
package com.zwz.thread.demo3;public class ThreadB extends Thread {	private ObjectService objectService;	public ThreadB(ObjectService objectService) {		super();		this.objectService = objectService;	}	@Override	public void run() {		objectService.setUserNamePassWord("b", "bb");	}}复制代码
package com.zwz.thread.demo3;public class MainTest {	public static void main(String[] args) {		ObjectService service=new ObjectService();		ThreadA a=new ThreadA(service);		a.setName("A");		a.start();		ThreadB b=new ThreadB(service);		b.setName("B");		b.start();	}}复制代码

运行结果:

下面我把String lock=new String();放在方法中会有啥结果了:

package com.zwz.thread.demo3;public class ObjectService {	private String uname;	private String pwd;	public void setUserNamePassWord(String userName,String passWord){		try {			String lock=new String();			synchronized (lock) {				System.out.println("thread name="+Thread.currentThread().getName()						+" 进入代码快:"+System.currentTimeMillis());				uname=userName;				Thread.sleep(3000);				pwd=passWord;				System.out.println("thread name="+Thread.currentThread().getName()						+" 进入代码快:"+System.currentTimeMillis()+"入参uname:"+uname+"入参pwd:"+pwd);			}		} catch (InterruptedException e) {			e.printStackTrace();		}	}}复制代码

运行结果:

结论:

多个线程持有对象监视器作为同一个对象的前提下,同一时间只有一个线程可以执行synchronized(任意自定义对象)同步代码快。 synchronized(任意自定义对象)与synchronized同步方法共用

package com.zwz.thread.demo4;public class ObjectService {	private String lock=new String();	public void methodA(){		try {			synchronized (lock) {				System.out.println("a begin");				Thread.sleep(3000);				System.out.println("a   end");			}		} catch (InterruptedException e) {			e.printStackTrace();		}	}	public synchronized void methodB(){		System.out.println("b begin");		System.out.println("b   end");	}}复制代码
package com.zwz.thread.demo4;public class ThreadA extends Thread {	private ObjectService objectService;	public ThreadA(ObjectService objectService) {		super();		this.objectService = objectService;	}	@Override	public void run() {		objectService.methodA();	}}复制代码
package com.zwz.thread.demo4;public class ThreadB extends Thread {	private ObjectService objectService;	public ThreadB(ObjectService objectService) {		super();		this.objectService = objectService;	}	@Override	public void run() {		objectService.methodB();	}}复制代码
package com.zwz.thread.demo4;public class MainTest {	public static void main(String[] args) {		ObjectService service=new ObjectService();		ThreadA a=new ThreadA(service);		a.setName("A");		a.start();		ThreadB b=new ThreadB(service);		b.setName("B");		b.start();	}	}复制代码

运行结果:

结论:

使用synchronized(任意自定义对象)进行同步操作,对象监视器必须是同一个对象。不过不是同一个,运行就是异步执行了。

静态同步synchronized方法与synchronized(*.class)代码块

静态同步synchronized方法

package com.zwz.thread.demo6;public class ObjectService {	public synchronized static void methodA(){		try {			System.out.println("static methodA begin 线程名称:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());			Thread.sleep(3000);			System.out.println("static methodA end   线程名称:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());		} catch (InterruptedException e) {			e.printStackTrace();		}	}		public synchronized static void methodB(){		System.out.println("static methodB begin 线程名称:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());		System.out.println("static methodB end   线程名称:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());	}}复制代码
package com.zwz.thread.demo6;public class ThreadA extends Thread {	@Override	public void run() {		ObjectService.methodA();	}}复制代码
package com.zwz.thread.demo6;public class ThreadB extends Thread {	@Override	public void run() {		ObjectService.methodB();	}}复制代码
package com.zwz.thread.demo6;public class MainTest {	public static void main(String[] args) {		ThreadA a=new ThreadA();		a.setName("A");		a.start();		ThreadB b=new ThreadB();		b.setName("B");		b.start();	}}复制代码

运行结果:

结论: synchronized应用在static方法上,那是对当前对应的*.Class进行持锁。 synchronized(*.class)代码块

package com.zwz.thread.demo7;public class ObjectService {	public void methodA(){		try {			synchronized (ObjectService.class) {				System.out.println("methodA begin 线程名称:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());				Thread.sleep(3000);				System.out.println("methodA end   线程名称:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());			}		} catch (InterruptedException e) {			e.printStackTrace();		}	}		public void methodB(){		synchronized (ObjectService.class) {			System.out.println("methodB begin 线程名称:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());			System.out.println("methodB end   线程名称:"+Thread.currentThread().getName()+" times:"+System.currentTimeMillis());		}	}}复制代码
package com.zwz.thread.demo7;public class ThreadA extends Thread {	private ObjectService objectService;	public ThreadA(ObjectService objectService) {		super();		this.objectService = objectService;	}	@Override	public void run() {		objectService.methodA();	}}复制代码
package com.zwz.thread.demo7;public class ThreadB extends Thread {	private ObjectService objectService;	public ThreadB(ObjectService objectService) {		super();		this.objectService = objectService;	}	@Override	public void run() {		objectService.methodB();	}}复制代码
package com.zwz.thread.demo7;public class MainTest {	public static void main(String[] args) {		ObjectService service=new ObjectService();		ThreadA a=new ThreadA(service);		a.setName("A");		a.start();		ThreadB b=new ThreadB(service);		b.setName("B");		b.start();	}}复制代码

运行结果:

上面测试方法是共同对象,下面我们分别实例化一个对象:

package com.zwz.thread.demo7;public class MainTest {	public static void main(String[] args) {		ObjectService service1=new ObjectService();		ObjectService service2=new ObjectService();		ThreadA a=new ThreadA(service1);		a.setName("A");		a.start();		ThreadB b=new ThreadB(service2);		b.setName("B");		b.start();	}}复制代码

运行结果:

结论: 同步synchronized(*.class)代码块的作用其实和synchronized static方法作用一样。Class锁对类的所有对象实例起作用。

参考:

转载于:https://juejin.im/post/5ba4a714f265da0afe62e017

你可能感兴趣的文章
windows下开发库路径解决方案
查看>>
linux迁移mysql数据目录
查看>>
脚本源码安装LNMP
查看>>
Percona Server安装
查看>>
函数为左边表达式
查看>>
读书杂谈一
查看>>
winform listbox 元素显示tooltrip
查看>>
cacti安装与配置
查看>>
TF-IDF与余弦相似性的应用(一):自动提取关键词
查看>>
javascript面向对象2
查看>>
限制容器对CPU的使用 - 每天5分钟玩转 Docker 容器技术(28)
查看>>
jquery 实现的一个 随机云标签网页背景
查看>>
RPC
查看>>
android广播事件处理broadcast receive
查看>>
在eclipse 里面 修改tomcat的配置--Server Locations
查看>>
网站 mvc url 路径 设置 为 *.html 的原因
查看>>
mybatis 开启使用 默认的 二级缓存
查看>>
docker 容器 创建和 使用
查看>>
SQLITE使用指南
查看>>
用Maven部署war包到远程Tomcat服务器
查看>>