您的位置:首页 > 其它

当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法?

2017-02-19 14:00 381 查看
分几种情况:
     1.其他方法前是否加了synchronized关键字,如果没加,则能。
     2.如果这个方法内部调用了wait,则可以进入其他synchronized方法。
     3.如果其他个方法都加了synchronized关键字,并且内部没有调用wait,则不能。
4.如果其他方法是static,它用的同步锁是当前类的字节码,与非静态的方法不能同步,因为非静态的方法用的是this。


当一个线程进入一个对象的synchronized()方法后,其他线程是否可以进入此对象的其他方法取决于方法本身,如果该方法是非synchronized()方法,那么是可以访问的,如果是synchronized()方法,那么不能访问。示例如下:

class MultiThread {
public synchronized void synchronizedMethod(){
System.out.println("begin calling synchronizedMethod...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("finish calling synchronizedMethod...");
}
public void generalMethod(){
System.out.println("call generalMethod...");
}

}

public class test34{
static final MultiThread t=new MultiThread();

      public static void main(String[] args) {
Thread t1=new Thread(){
public void run(){
t.synchronizedMethod();
}
};
Thread t2=new Thread(){
public void run(){
t.generalMethod();
}
};
t1.start();
t2.start();

      }

}
结果为:
begin calling synchronizedMethod...
call generalMethod...

finish calling synchronizedMethod...

从上例可以看出,线程t1在调用synchronized()方法的过程中,线程t2仍然可以访问同一个对象的非sychronized()方法。

在方法generalMethod()前加一个synchronized的修饰符。因为线程t1调用synchronized
void synchronizedMethod()的时候(即线程t1获得了对象锁),t2是无法调用该方法的。修改后的代码如下:

class MultiThread {
public synchronized void synchronizedMethod(){
System.out.println("begin calling synchronizedMethod...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("finish calling synchronizedMethod...");
}
public synchronized void generalMethod(){
System.out.println("call generalMethod...");
}

}

public class test34{
static final MultiThread t=new MultiThread();

      public static void main(String[] args) {
Thread t1=new Thread(){
public void run(){
t.synchronizedMethod();
}
};
Thread t2=new Thread(){
public void run(){
t.generalMethod();
}
};
t1.start();
t2.start();

      }

}

结果为:
begin calling synchronizedMethod...

finish calling synchronizedMethod...

call generalMethod...

如果其他方法是静态的方法,它用的同步锁是当前类的字节码,与非静态的方法不能同步,因此,静态方法可以被调用,实例如下:

class MultiThread {
public synchronized void synchronizedMethod(){
System.out.println("begin calling synchronizedMethod...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("finish calling synchronizedMethod...");
}
public synchronized static void generalMethod(){
System.out.println("call generalMethod...");
}

}

public class test34{
static final MultiThread t=new MultiThread();

      public static void main(String[] args) {
Thread t1=new Thread(){
public void run(){
t.synchronizedMethod();
}
};
Thread t2=new Thread(){
public void run(){
t.generalMethod();
}
};
t1.start();
t2.start();

      }

}

结果为:
begin calling synchronizedMethod...

call generalMethod...

finish calling synchronizedMethod...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐