两个线程执行两个“同步"方法? [英] Two threads executing two `synchronized` methods?
问题描述
我正在阅读有关JAVA同步的信息.
I was reading about JAVA synchronization.
我的课堂上有2种方法.
I have 2 methods in my class.
public synchronized void eat()
{
System.out.println("eat");
eatDinner();
}
public synchronized void eatDinner()
{
System.out.println("eat");
}
我的两个方法都是同步的.
Both of my methods are synchronized.
现在是否有2个线程可以同时调用eat()
和另一个eatDinner()
来运行?
并且如果thread2仍未执行eatDinner()
.线程1可以从eat()
调用eatDinner()
吗?
Now Is it possible for 2 threads one is calling eat()
and another eatDinner()
to run simultaneously?
And If thread2 has not still executing eatDinner()
. Can thread1 can call eatDinner()
from eat()
?
推荐答案
否,两个线程不可能同时运行方法eat
和eatDinner
. (注意:只要在类的 same 实例上调用这些方法)
No, it is not possible for two threads to run the methods eat
and eatDinner
simultaneously. (Caveat: as long as these methods are invoked on the same instance of the class)
synchronized
关键字应用于非静态方法时,将在对象本身上进行同步.
The synchronized
keywords, when applied to a non-static method, synchronizes on the object itself.
您的代码可以在不更改含义的情况下被重写为:
Your code can be rewritten, without changing the meaning, as:
public void eat() {
synchronized (this) {
System.out.println("eat");
eatDinner();
}
}
public void eatDinner() {
synchronized (this) {
System.out.println("eat");
}
}
这可能更容易使他们看到它们都在同一个 monitor 上同步. 每个java对象都有一个 monitor .
This probably makes it easier to see that they are both synchronizing on the same monitor. Each java object has a monitor.
只要'thread1'持有对象的监视器,它就可以在同一监视器上输入其他synchronized
块. "thread1"必须退出所有同步块(存在的块数与进入它们的次数相同),然后另一个线程才能获得监视器的所有权.
As long as 'thread1' is holding the monitor of your object, it can enter other synchronized
blocks on the same monitor. 'thread1' has to exit all synchronized blocks (exist the blocks as many times as it has entered them) before another thread can take ownership of the monitor.
因此,如果thread1已在eat
方法中,则线程1可以调用eatDinner
-没问题.但是,如果thread2
当前在eat
方法中,则thread1
将在调用eatDinner
时阻塞,直到线程2同时完成eatDinner
和eat
.
So thread1 can call eatDinner
if it is already in the eat
method - no problem. But if thread2
is currently in the eat
method, then thread1
will block when it calls eatDinner
until thread2 has finished both eatDinner
and eat
.
添加: 回应您的评论
@Raj:如果两个线程是由相同的类实例创建的,那么?
@Raj: If two threads are created by same class instances, then?
创建线程的方式并不重要-发生在同一类中还是在代码中完全不同的位置无关紧要.两个不同的线程始终是独立的.
It is not important how the threads were created - it doesn't matter if that happened from within the same class or from completely different locations in your code. Two different threads are always independent.
仅在同步哪个对象的监视器上很重要:每个对象实例都有一个监视器".您看不到此监视器-它没有名称,但它在那里,被synchronized
关键字以及java.lang.Object
中定义的wait
,notify
和notifyAll
方法使用.
It only matters on which object's monitor you synchronize: each object instance has one 'monitor'. You can't see this monitor - it doesn't have a name, but it's there and it is used by the synchronized
keywords and by the wait
, notify
and notifyAll
methods defined in java.lang.Object
.
这篇关于两个线程执行两个“同步"方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!