java中几种synchronized用法总结
来源:互联网 发布:网络机房整改方案 编辑:程序博客网 时间:2024/06/05 00:33
synchronized方法,synchronized静态方法,synchronized(this),synchronized(类),synchronized(变量)之间的区别
先贴代码
/** * Created by sunshanpeng on 2016/9/27. */public class ThreadTest { public static final String Lock = "lock"; public final Object obj = new Object(); synchronized void test1() throws InterruptedException { for (int i = 0; i < 5; i++) { Thread.sleep(1000); System.out.println("test1="+i); } } synchronized static void test2() throws InterruptedException { for (int i = 0; i < 5; i++) { Thread.sleep(1000); System.out.println("test2=" + i); } } void test3() throws InterruptedException { synchronized (this) { for (int i = 0; i < 5; i++) { Thread.sleep(1000); System.out.println("test3=" + i); } } } void test4() throws InterruptedException { synchronized (this.getClass()) { for (int i = 0; i < 5; i++) { Thread.sleep(1000); System.out.println("test4=" + i); } } } void test5() throws InterruptedException { synchronized (Lock) { for (int i = 0; i < 5; i++) { Thread.sleep(1000); System.out.println("test5=" + i); } } } void test6() throws InterruptedException { synchronized (obj) { for (int i = 0; i < 5; i++) { Thread.sleep(1000); System.out.println("test6=" + i); } } }}
一、同一个对象的同一个同步方法是同步的
这个毋庸置疑,不多解释
二、同一个对象的synchronized方法和synchronized(this)块是同步的
方法test1和test3同步
这两个方法的锁都是加在当前对象上
三、类的static方法和synchronized(该类)块是同步的
方法test2和test4同步
这两个方法的锁都是加在ThreadTest类上面,方法test4的synchronized (this.getClass())块换成synchronized (ThreadTest.class),结果是一样的,但是如果换成比如synchronized (String.class)就不能同步了
因为锁是加在类上面,所以不仅相同类的这两种方法是同步的,同一个类的不同对象之间的这两种方法也是同步的
四、第二点的两种方法和第三点的两种方法不同步
test1和test2不同步,test1和test4不同步
test3和test2不同步,test3和test4不同步
五、synchronized(变量)跟前面四种都不同步
因为锁加的目标不一样,既不是当前类也不是当前对象。
synchronized (obj)和synchronized (this)的用法基本一致,对当前对象加锁。
而synchronized (Lock)除了当前对象以外,本类的其他新建对象也会同步加锁,甚至于其他类如果调用的synchronized (ThreadTest.Lock)也会同步。
比如:
class ThreadTest2{ void test7() throws InterruptedException { synchronized (ThreadTest.Lock) { for (int i = 0; i < 5; i++) { Thread.sleep(1000); System.out.println("test7=" + i); } } }}
ThreadTest2的test7方法跟ThreadTest的test5方法是同步的;
测试代码:
public static void main(String[] args) throws Exception{ final ThreadTest t1 = new ThreadTest(); final ThreadTest t2 = new ThreadTest(); new Thread(){ public void run() { try { t1.test5(); } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); new Thread(){ public void run() { try { new ThreadTest2().test7(); } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); }
语言组织的不是很好,因为网上没有其他相关文献,所以写一篇来抛砖引玉。
0 0
- java中几种synchronized用法总结
- java synchronized用法总结
- java中synchronized用法基本总结
- java的关键字synchronized用法总结
- synchronized 用法总结
- synchronized 用法总结 .
- synchronized 用法总结
- synchronized 用法总结
- synchronized 修饰词用法总结。
- java多线程中的synchronized和wait/notify用法总结
- Java多线程编程——synchronized用法总结
- java中synchronized用法
- java中synchronized用法
- java中synchronized用法
- java里面synchronized用法
- java中synchronized用法
- Java synchronized用法
- java中synchronized用法
- 互联网巨头亚马逊公司微服务化经验
- Jquery easyui tree
- iOS中的界面渲染性能
- SqlServer
- 【BZOJ4566】找相同字符,后缀数组
- java中几种synchronized用法总结
- 系统设置指令
- redis事务
- 第五周项目2-建立链栈算法库
- iOS各种被拒的原因汇总
- Android - WebView 加载 Https 出现 SSL Error. Failed to validate the certificate chain
- JDBC----Result结果集
- 使用javascript原生实现一个模板引擎
- zedboard的u-boot编译---编辑自己的配置文件