线程同步问题

1.当一个同步方法已经执行,线程能够调用对象上的非同步实例方法吗?

可以,一个非同步方法总是可以被调用而不会有任何问题。实际上,Java 没有为非同步方法做任何检查,锁对象仅仅在同步方法或者同步代码块中检查。如果一个方法没有声明为同步,即使你在使用共享数据 Java 照样会调用,而不会做检查是否安全,所以在这种情况下要特别小心。一个方法是否声明为同步取决于临界区访问(critial section access),如果方法不访问临界区(共享资源或者数据结构)就没必要声明为同步的。 

下面有一个示例说明:Common 类有两个方法 synchronizedMethod1()和 method1(),MyThread 类在独立的线程中调用这两个方法。  --------------------- 本文来自 舞动de人生 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/dongwujing/article/details/7647858?utm_source=copy 。

  • public class Common {  
  •   
  • public synchronized void synchronizedMethod1() {  
  • System.out.println (“synchronizedMethod1 called”);  
  • try {  
  • Thread.sleep (1000);  
  • catch (InterruptedException e) {  
  • e.printStackTrace ();  
  • }  
  • System.out.println (“synchronizedMethod1 done”);  
  • }  
  • public void method1() {  
  • System.out.println (“Method 1 called”);  
  • try {  
  • Thread.sleep (1000);  
  • catch (InterruptedException e) {  
  • e.printStackTrace ();  
  • }  
  • System.out.println (“Method 1 done”);  
  • }  
  • }  
  • public class MyThread extends Thread {  
  • private int id = 0;  
  • private Common common;  
  •   
  • public MyThread (String name, int no, Common object) {  
  • super(name);  
  • common = object;  
  • id = no;  
  • }  
  •   
  • public void run () {  
  • System.out.println (“Running Thread” + this.getName ());  
  • try {  
  • if (id == 0) {  
  • common.synchronizedMethod1();  
  • else {  
  • common.method1();  
  • }  
  • catch (Exception e) {  
  • e.printStackTrace ();  
  • }  
  • }  
  •   
  • public static void main (String[] args) {  
  • Common c = new Common ();  
  • MyThread t1 = new MyThread (“MyThread-1″, 0, c);  
  • MyThread t2 = new MyThread (“MyThread-2″, 1, c);  
  • t1.start ();  
  • t2.start ();  
  • }
  • }

输出结果

  • Running ThreadMyThread-1  
  • synchronizedMethod1 called  
  • Running ThreadMyThread-2  
  • Method 1 called  
  • synchronizedMethod1 done  
  • Method 1 done 

--------------------- 本文来自 舞动de人生 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/dongwujing/article/details/7647858?utm_source=copy 

2.在一个对象上两个线程可以调用两个不同的同步实例方法吗? 

不能,因为一个对象已经同步了实例方法,线程获取了对象的对象锁。所以只有执行完该方法释放对象锁后才能执行其它同步方法。看下面代码示例非常清晰:Common 类有 synchronizedMethod1()和 synchronizedMethod2()方法,MyThread 调用这两个方法。

  • public class Common {  
  • public synchronized void synchronizedMethod1() {  
  • System.out.println (“synchronizedMethod1 called”);  
  • try {  
  • Thread.sleep (1000);  
  • catch (InterruptedException e) {  
  • e.printStackTrace ();  
  • }  
  • System.out.println (“synchronizedMethod1 done”);  
  • }  
  •   
  • public synchronized void synchronizedMethod2() {  
  • System.out.println (“synchronizedMethod2 called”);  
  • try {  
  • Thread.sleep (1000);  
  • catch (InterruptedException e) {  
  • e.printStackTrace ();  
  • }  
  • System.out.println (“synchronizedMethod2 done”);  
  • }  
  • }  
  • public class MyThread extends Thread {  
  • private int id = 0;  
  • private Common common;  
  •   
  • public MyThread (String name, int no, Common object) {  
  • super(name);  
  • common = object;  
  • id = no;  
  • }  
  •   
  • public void run () {  
  • System.out.println (“Running Thread” + this.getName ());  
  • try {  
  • if (id == 0) {  
  • common.synchronizedMethod1();  
  • else {  
  • common.synchronizedMethod2();  
  • }  
  • catch (Exception e) {  
  • e.printStackTrace ();  
  • }  
  • }  
  •   
  • public static void main (String[] args) {  
  • Common c = new Common ();  
  • MyThread t1 = new MyThread (“MyThread-1″, 0, c);  
  • MyThread t2 = new MyThread (“MyThread-2″, 1, c);  
  • t1.start ();  
  • t2.start ();  
  • }  
  • }  

----------

输出结果

Running ThreadMyThread-1
Running ThreadMyThread-2
synchronizedMethod1 called
synchronizedMethod1 done
synchronizedMethod2 called
synchronizedMethod2 done

 

----------- 本文来自 舞动de人生 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/dongwujing/article/details/7647858?utm_source=copy 

转载于:https://www.cnblogs.com/toby-ma/p/9754403.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值