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