Synchroniz用法
来源:互联网 发布:中国原油进口数据比较 编辑:程序博客网 时间:2024/06/04 00:23
1.同一个类的synchronized 锁住的方法,同一时间不能有多个线程同时访问
package com.mercurylake.test.tree;import java.util.HashSet;import java.util.Set;import org.apache.commons.lang.StringUtils;import com.sun.swing.internal.plaf.synth.resources.synth;public class A {public static void main(String[] args) {A x=new A();Runnable r1=new B(x,0);Runnable r2=new B(x,1);Thread t1=new Thread(r1);Thread t2=new Thread(r2);t1.start();t2.start();}public synchronized void a1(){System.out.println("a1..begin");try {Thread.sleep(10000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("a1..end");}public synchronized void b1(){System.out.println("b1");}}class B implements Runnable{private A a;private int mark;public B(A a, int mark) {super();this.a = a;this.mark = mark;}@Overridepublic void run() {// TODO Auto-generated method stubif(mark==0){a.a1();}else a.b1();}}
返回值:a1..begin ;a1..end ;b1
2.synchronized(this)代码块与 public synchronized void a() 效果相同
package com.mercurylake.test.tree;import java.util.HashSet;import java.util.Set;import org.apache.commons.lang.StringUtils;import com.sun.swing.internal.plaf.synth.resources.synth;public class A {public static void main(String[] args) {A x=new A();Runnable r1=new B(x,0);Runnable r2=new B(x,1);Thread t1=new Thread(r1);Thread t2=new Thread(r2);t1.start();try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}t2.start();}public synchronized void a1(){System.out.println("a1..begin");try {Thread.sleep(10000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("a1..end");}public void b1(){synchronized (this) {System.out.println("b1");}}}class B implements Runnable{private A a;private int mark;public B(A a, int mark) {super();this.a = a;this.mark = mark;}@Overridepublic void run() {// TODO Auto-generated method stubif(mark==0){a.a1();}else a.b1();}}输出值与例1中相同
3.synchronized static 用法:锁的不再是实例,而是Class,同一个Class 都会受到同步约束
package com.mercurylake.test.tree;import java.util.HashSet;import java.util.Set;import org.apache.commons.lang.StringUtils;import com.sun.swing.internal.plaf.synth.resources.synth;public class A {public static void main(String[] args) {A x=new A();A y=new A();Runnable r1=new B(x,0);Runnable r2=new B(y,1);Thread t1=new Thread(r1);Thread t2=new Thread(r2);t1.start();try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}t2.start();}public synchronized static void a1(){System.out.println("a1..begin");try {Thread.sleep(10000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("a1..end");}public synchronized static void b1(){System.out.println("b1");}}class B implements Runnable{private A a;private int mark;public B(A a, int mark) {super();this.a = a;this.mark = mark;}@Overridepublic void run() {// TODO Auto-generated method stubif(mark==0){a.a1();}else a.b1();}}返回值与例1相同
0 0
- Synchroniz用法
- 生产者和消费者成功解答synchroniz…
- 生产者和消费者成功解答synchroniz…
- 抽象方法不能是static或native或synchroniz
- 抽象方法不能是static或native或synchroniz
- 抽象方法不能是static或native或synchroniz
- 抽象方法不能是static或native或synchroniz
- 抽象方法不能是static或native或synchroniz
- 抽象方法不能是static或native或synchroniz
- 抽象方法不能是static或native或synchroniz
- 抽象方法不能是static或native或synchroniz
- Hibernate配置文件错误提示,The content of element type "list" must match "(meta*,subselect?,cache?,synchroniz
- 抽象方法不能是static或native或synchroniz 原因及误解
- 用法
- ,, 用法
- 用法
- 用法
- #用法
- 移位运算
- mysql无法启动 1067
- S2_二本第三章使用集合组织相关数据(非泛型)
- LeetCode 77. Combinations
- 安装Cocoapods, 更新gem出现的问题
- Synchroniz用法
- Visual Studio配置(6):项目属性页-C/C++(2)
- linux 配置apache
- 原生JS获取HTML样式并修改
- Redis事务使用方法
- leecode 解题总结:99. Recover Binary Search Tree
- ijkplayer读取rtp包过程
- 进程组、会话、守护进程
- 集思广益