java 中关于synchronized的通常用法
package j2se.thread.test; /***
* synchronized(class)很特别,它会让另一个线程在任何需要获取class做为monitor的地方等待.
* class与this做为不同的监视器可以同时使用,不存在一个线程获取了class,另一个线程就不能获取该class的一切实例.
根据下面的代码自行修改,分别验证下面的几种情况:
synchronized(class)
synchronized(this)
->线程各自获取monitor,不会有等待.
synchronized(this)
synchronized(this)
->如果不同线程监视同一个实例对象,就会等待,如果不同的实例,不会等待.
synchronized(class)
synchronized(class)
->如果不同线程监视同一个实例或者不同的实例对象,都会等待.
*/ /**
* @author liwei
测试synchronized(this)与synchronized(class)
*/
public class TestSynchronied8 {
private static byte[] lockStatic = new byte[0]; // 特殊的instance变量
private byte[] lock = new byte[0]; // 特殊的instance变量 public synchronized void m4t5() {
System.out.println(this);
int i = 50;
while( i-- > 0) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
}
Thread.yield();
}
} public void m4t0() {
synchronized(this) {
System.out.println(this);
int i = 50;
while( i-- > 0) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
}
Thread.yield();
}
}
} public void m4t1() {
synchronized(lock) {
System.out.println(this);
int i = 50;
while( i-- > 0) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
}
Thread.yield();
}
}
}
/**
* synchronized(class)
synchronized(class)
->如果不同线程监视同一个实例或者不同的实例对象,都会等待.
*/
public static synchronized void m4t2() {
int i = 50;
while( i-- > 0) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
}
Thread.yield(); }
} public static void m4t3() {
synchronized(TestSynchronied8.class){
int i = 50;
while( i-- > 0) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
}
Thread.yield();
}
}
} public static void m4t4() {
synchronized(lockStatic){
int i = 50;
while( i-- > 0) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
}
Thread.yield();
}
}
} /**
synchronized(this)
synchronized(this)
->如果不同线程监视不同的实例,不会等待.
*/
public static void testObjsyn1(){
final TestSynchronied8 myt2 = new TestSynchronied8();
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
System.out.println("测试两个不同的对象上,运行同一个synchronized(this)代码块-------------------------------------");
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t1 = new Thread( new Runnable() { public void run() { myt1.m4t0(); } }, "t1" );
Thread t2 = new Thread( new Runnable() { public void run() { myt2.m4t0(); } }, "t2" );
t1.start();
t2.start();
} /**
synchronized(this)
synchronized(this)
->如果不同线程监视同一个实例,会等待.
*/
public static void testObjsyn2(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
System.out.println("测试一个对象上,运行同一个synchronized(this)代码块-------------------------------------");
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t3 = new Thread( new Runnable() { public void run() { myt1.m4t0(); } }, "t3" );
Thread t4 = new Thread( new Runnable() { public void run() { myt1.m4t0(); } }, "t4" );
t3.start();
t4.start();
} /**
synchronized(this)
synchronized(this)
->如果不同线程监视同一个实例,会等待.
*/
public static void testObjsyn3(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
System.out.println("测试一个对象上,运行同一个synchronized(obj)代码块-------------------------------------");
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t5 = new Thread( new Runnable() { public void run() { myt1.m4t1(); } }, "t5" );
Thread t6 = new Thread( new Runnable() { public void run() { myt1.m4t1(); } }, "t6" );
t5.start();
t6.start();
} /**
synchronized(this)
synchronized(this)
->如果不同线程监视同一个实例,会等待.
*/
public static void testObjsyn4(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
System.out.println("测试一个对象上,运行同一个synchronized方法-------------------------------------");
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t7 = new Thread( new Runnable() { public void run() { myt1.m4t5(); } }, "t7" );
Thread t8 = new Thread( new Runnable() { public void run() { myt1.m4t5(); } }, "t8" );
t7.start();
t8.start();
} /**
synchronized(this)
synchronized(this)
->如果不同线程监视不同的实例,不会等待.
*/
public static void testObjsyn5(){
final TestSynchronied8 myt1 = new TestSynchronied8();
final TestSynchronied8 myt2 = new TestSynchronied8();
try {
System.out.println("测试两个不同对象上,运行同一个synchronized方法-------------------------------------");
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t9 = new Thread( new Runnable() { public void run() { myt1.m4t5(); } }, "t9" );
Thread t10 = new Thread( new Runnable() { public void run() { myt2.m4t5(); } }, "t10" );
t9.start();
t10.start();
} /**
synchronized(this)
synchronized方法
->如果不同线程监视同一实例,一个是syschronized(this),一个是同步方法,会等待.
*/
public static void testObjsyn6(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t9 = new Thread( new Runnable() { public void run() { myt1.m4t0(); } }, "t9" );
Thread t10 = new Thread( new Runnable() { public void run() { myt1.m4t5(); } }, "t10" );
t9.start();
t10.start();
} /**
synchronized(lock)
synchronized方法
->如果不同线程监视同一实例,一个是syschronized(lock),一个是同步方法,不会等待.
*/
public static void testObjsyn7(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t9 = new Thread( new Runnable() { public void run() { myt1.m4t1(); } }, "t9" );
Thread t10 = new Thread( new Runnable() { public void run() { myt1.m4t5(); } }, "t10" );
t9.start();
t10.start();
} /**
synchronized(lock)
synchronized方法
->如果不同线程监视同一实例,一个是syschronized(lock),一个是syschronized(this),不会等待.
*/
public static void testObjsyn71(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t9 = new Thread( new Runnable() { public void run() { myt1.m4t0(); } }, "t9" );
Thread t10 = new Thread( new Runnable() { public void run() { myt1.m4t1(); } }, "t10" );
t9.start();
t10.start();
} /**
* synchronized(class)
synchronized(class)
->如果不同线程监视不同的实例对象,会等待.
*/
public static void testObjsyn8(){
final TestSynchronied8 myt1 = new TestSynchronied8();
final TestSynchronied8 myt2 = new TestSynchronied8();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t2 = new Thread( new Runnable() { public void run() { myt2.m4t2(); } }, "t2" );
Thread t4 = new Thread( new Runnable() { public void run() { myt1.m4t2(); } }, "t4" );
t2.start();
t4.start();
} /**
* synchronized(class)
synchronized(class)
->如果不同线程监视同一的实例对象,会等待.
*/
public static void testObjsyn9(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t2 = new Thread( new Runnable() { public void run() { myt1.m4t2(); } }, "t2" );
Thread t4 = new Thread( new Runnable() { public void run() { myt1.m4t2(); } }, "t4" );
t2.start();
t4.start();
} /**
* synchronized(class)
synchronized(this)
->线程各自获取monitor,不会有等待.
*/
public static void testObjsyn10(){
final TestSynchronied8 myt1 = new TestSynchronied8();
final TestSynchronied8 myt2 = new TestSynchronied8();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t7 = new Thread( new Runnable() { public void run() { myt2.m4t0(); } }, "t7" );
Thread t8 = new Thread( new Runnable() { public void run() { myt1.m4t3(); } }, "t8" );
t7.start();
t8.start();
} /**
* synchronized(class)
synchronized(this)
->线程各自获取monitor,不会有等待.
*/
public static void testObjsyn11(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t7 = new Thread( new Runnable() { public void run() { myt1.m4t0(); } }, "t7" );
Thread t8 = new Thread( new Runnable() { public void run() { myt1.m4t3(); } }, "t8" );
t7.start();
t8.start();
} /**
* synchronized(class)
synchronized(lock)
->线程各自获取monitor,不会有等待.
*/
public static void testObjsyn12(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t7 = new Thread( new Runnable() { public void run() { myt1.m4t1(); } }, "t7" );
Thread t8 = new Thread( new Runnable() { public void run() { myt1.m4t3(); } }, "t8" );
t7.start();
t8.start();
} /**
* synchronized(class)
synchronized(lockStatic)
->线程各自获取monitor,不会有等待.
*/
public static void testObjsyn13(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t7 = new Thread( new Runnable() { public void run() { myt1.m4t4(); } }, "t7" );
Thread t8 = new Thread( new Runnable() { public void run() { myt1.m4t3(); } }, "t8" );
t7.start();
t8.start();
} /**
* synchronized方法
synchronized(lockStatic)
->线程各自获取monitor,不会有等待.
*/
public static void testObjsyn14(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t7 = new Thread( new Runnable() { public void run() { myt1.m4t4(); } }, "t7" );
Thread t8 = new Thread( new Runnable() { public void run() { myt1.m4t2(); } }, "t8" );
t7.start();
t8.start();
} /**
* synchronized方法
synchronized(lockStatic)
->线程各自获取monitor,不会有等待.
*/
public static void testObjsyn15(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t7 = new Thread( new Runnable() { public void run() { myt1.m4t3(); } }, "t7" );
Thread t8 = new Thread( new Runnable() { public void run() { myt1.m4t2(); } }, "t8" );
t7.start();
t8.start();
} /**
* synchronized方法
synchronized(lockStatic)
->线程各自获取monitor,不会有等待.
*/
public static void testObjsyn16(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t7 = new Thread( new Runnable() { public void run() { myt1.m4t1(); } }, "t7" );
Thread t8 = new Thread( new Runnable() { public void run() { myt1.m4t4(); } }, "t8" );
t7.start();
t8.start();
} /**
* synchronized(class)和static synchronized 方法所获取的锁是一样的
* synchronized(class)方法和静态的方法中的 synchronized(lockStatic)代码块获取的锁是不一样的
* 非静态的方法中的synchronized(this)代码块和非静态的 synchronized 方法所获取的锁是一样的
* (静态和非静态) synchronized(this)和(静态和非静态)的 synchronized(lock)代码块获取的锁是不一样的
*
* 总的来说synchronized(class)和static synchronized 方法获取的是类锁
* 非静态的方法中的synchronized(this)代码块和非静态的 synchronized 方法所获取的锁是类的实例的对象锁
* synchronized(lockStatic)获取的是静态属性的锁
* synchronized(lock) 获取的是非静态属性的锁
* 如果获取的锁是一样的,代码就会同步 ;锁不一样就不会同步
*/
public static void main(String[] args) {
//对于非静态的方法,同步方法和 synchronized(this) 获取的是实例对象锁
//对于非静态的方法,同步方法和 synchronized(lock) 获取的锁的lock // testObjsyn1();//如果不同线程监视不同的实例,不会等待. synchronized(this)
// testObjsyn2();//如果不同线程监视同一个实例,会等待. synchronized(this)
// testObjsyn3();//如果不同线程监视同一个实例,会等待. synchronized(lock)
// testObjsyn4();//如果不同线程监视同一个实例,会等待. synchronized 方法
// testObjsyn5();//如果不同线程监视不同的实例,不会等待. synchronized 方法
// testObjsyn6();//如果不同线程监视同一实例,一个是syschronized(this),一个是同步方法,会等待
// testObjsyn7(); //如果不同线程监视同一实例,一个是syschronized(lock),一个是同步方法,不会等待,锁定的对象不一样的
// testObjsyn71(); //如果不同线程监视同一实例,一个是syschronized(lock),一个是syschronized(this),不会等待,锁定的对象不一样的 /**
* synchronized(class)
synchronized(class)
->如果不同线程监视同一个实例或者不同的实例对象,都会等待.
*/
// testObjsyn8();//如果不同线程监视不同的实例对象,会等待.synchronized(class)
// testObjsyn9();//如果不同线程监视同一的实例对象,会等待.synchronized(class)
// testObjsyn10();//如果不同线程监视不同的实例对象,不会等待.synchronized(class),synchronized(this)
// testObjsyn11();//如果不同线程监视同一的实例对象,不会等待.synchronized(class),synchronized(this)
// testObjsyn12();//如果不同线程监视同一的实例对象,不会等待.synchronized(class),synchronized(lock)
// testObjsyn13();//如果不同线程监视同一的实例对象,不会等待.synchronized(class),synchronized(lockStatic)
// testObjsyn14();//如果不同线程监视同一的实例对象,不会等待.synchronized()方法,synchronized(lockStatic)
// testObjsyn15();//如果不同线程监视同一的实例对象,会等待.synchronized()方法,synchronized(class)
// testObjsyn16();//如果不同线程监视同一的实例对象,不会等待.synchronized(lock)方法,synchronized(lockStatic) }
}
java 中关于synchronized的通常用法的更多相关文章
- 巨人大哥谈Java中的Synchronized关键字用法
巨人大哥谈Java中的Synchronized关键字用法 认识synchronized 对于写多线程程序的人来说,经常碰到的就是并发问题,对于容易出现并发问题的地方价格synchronized基本上就 ...
- JAVA中字符串函数subString的用法小结
本篇文章主要是对JAVA中字符串函数subString的用法进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助 String str; str=str.substring(int begi ...
- 【Java学习笔记之二十九】Java中的"equals"和"=="的用法及区别
Java中的"equals"和"=="的用法及区别 在初学Java时,可能会经常碰到下面的代码: String str1 = new String(" ...
- java中 this 的三种用法
Java中this的三种用法 调用属性 (1)this可以调用本类中的任何成员变量 调用方法(可省略) (2)this调用本类中的成员方法(在main方法里面没有办法通过this调用) 调用构造方法 ...
- Java中枚举的写法和用法
在公司代码中,用了一大堆的枚举,看得我好懵逼.下面开始看看枚举怎么写和怎么用. 一.枚举的写法 关于枚举的写法,网上好多这方面的知识.这里直接贴一个我自己写的枚举类的代 ...
- Java中try,catch,finally的用法
Java中try,catch,finally的用法,以前感觉还算熟悉,但看到一篇博文才有更深点的理解,总结网友博客如下. Java异常处理的组合方式: 1.try+catch 运行流程:运行到try ...
- 对kotlin和java中的synchronized的浅谈
synchronized在java中是一个关键字,但是在kotlin中是一个内联函数.假如分别在java和kotlin代码锁住同一个对象,会发生什么呢,今天写了代码试了试.首先定义people类 12 ...
- Java中的Synchronized关键字用法
认识synchronized 对于写多线程程序的人来说,经常碰到的就是并发问题,对于容易出现并发问题的地方加上synchronized修饰符基本上就搞定 了,如果说不考虑性能问题的话,这一招绝对能应对 ...
- 深入理解java中的synchronized关键字
synchronized 关键字,代表这个方法加锁,相当于不管哪一个线程A每次运行到这个方法时,都要检查有没有其它正在用这个方法的线程B(或者C D等),有的话要等正在使用这个方法的线程B(或者C D ...
随机推荐
- Fiddler 4 抓包
Fiddler 4 Tools –> Fiddler Options HTTPS -> 勾选"CaptureHTTPS CONNECTs",同时勾选"Decr ...
- 一、Hello World
刚创建的Android项目是一个基础的Hello World项目,包含一些默认文件,我们需要花一点时间看看最重要的部分: activity_main.xml -- 这是刚才用Android Stud ...
- 生成yyMMddHHmmssSS时间戳代码作为唯一主键值
import java.sql.Time; import java.text.DateFormat; import java.text.ParseException; import java.text ...
- point
7.10 []getch->_getch []错误:fatal error C1189: #error : EasyX is only for C++ :.c改为.cpp
- centos 开机启动服务
一.启动脚本 /etc/rc.local 启动 最简单的一种方式,在启动脚本 /etc/rc.local (其实 /etc/rc.local 是/etc/rc.d/rc.local 的软链接文件,实际 ...
- stm32定时器中断类型分析
一直在用的stm32定时器的中断都是TIM_IT_Update更新中断,也没问为什么,直到碰到有人使用TIM_IT_CC1中断,才想到这定时器的中断类型究竟有什么区别,都怪当时学习stm32的时候不够 ...
- IE下,js改变绝对定位的属性不生效
如果想要动态改变定位位置的值,例如改变TOP的值,IE下必须要给TOP一个初始值,不然不生效..
- OpenGL利用模板测试实现不规则裁剪
本文是原创文章,如需转载,请注明文章出处 在游戏开发中,经常会有这样的需求:给定一张64x64的卡牌素材,要求只显示以图片中心为圆点.直径为64的圆形区域,这就要用到模板测试来进行不规则裁剪. 实现不 ...
- Android 常用布局视图
常用包 http://square.github.io/ EventBus Scroller 滚动 拖拽 # android.support.design.widget.CollapsingToolb ...
- 两个div叠加触发事件发生闪烁问题
今天遇到一个问题,想实现一个功能: 当鼠标移到div1上的时候,会出现div2.出现时div2在div1的上面,div2在出现后发生闪烁的问题. 于是开始找问题根源,发现原来是因为当我们触发div1的 ...