【发布时间】:2016-03-11 20:00:09
【问题描述】:
我正在寻找一种方法来避免这两种方法同时运行,但不停止它们中的一种(或两种)以自行同时运行。例如,在下面的代码中,我必须让方法 A 在同一个对象上同时运行,但方法 B 应该阻塞,直到没有线程运行方法 A。同样,如果 B 在任何线程上运行并且 B 可以运行,则 A 应该阻塞与自身同时发生。
public void A() // calls from different threads on the same object allowed
{
....
} // A should only block if B is running
public void B() // calls from different threads on the same object allowed
{
....
} //B should only block if A is active
【问题讨论】:
标签: java multithreading methods concurrency synchronized