在线程上使用 getState() 方法 - Java
假设我们有两个线程 A、B 和一个互斥体(共享资源)M。
我启动 A 线程 (a.start()),它将调用 M 中的同步方法并且它导致Await()。 A 进入 wait() 后,线程 B (b) 如何跟随 A ?
谢谢
编辑:
是否有一种与join()“类似”的方法,其中线程b将加入在启动a时启动的线程等待状态? (正如我发现的, join() 将在线程完成时发生,但我不希望这种情况发生
Lets say we have two Threads A, B and one Mutex (Shared resource) M.
I start the A thread (a.start()), and it will call a synchronized method in M and it causes A to wait(). How can the thread B (b) follow A after A enters wait() ?
thanks
EDIT:
Is there a method "similar" to join() in which the thread b will join the thread started a when it is in WAITING state? (As I found , join() will happen when the thread finishes, but i don't want that to happen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
线程“t”中的对象如果在同步块中,则可以调用 wait,jvm 将管理正在运行的同步块的执行... --- 其实很简单,正在等待的对象将获取该同步块的锁当相反的对象屈服时,线程执行,并开始工作......所以,简而言之,如果您的对象 B 将开始运行,如果它:
1)与 A 位于同一个线程中
2)当 A 停止时正在“wait()” running
3) 在同步块中执行
A object in thread "t" can call wait if in a synchronized block, and the jvm will manage the execution of synchronous blocks that are running... --- It's actually quite simple, objects that are waiting will grab the lock of that threads execution when an opposing object yields, and start working.... So, in short, your object B will start running if it:
1) is in the same thread as A
2) is "wait()"ing when A stops running
3) is executing in a synchronous block