简体   繁体   English

没有线程阻塞的主线程回调(Java)

[英]Callbacks to main thread without thread blocking (Java)

I have a program which should managing threads (create, interrupt and show working threads). 我有一个应该管理线程的程序(创建,中断和显示工作线程)。 And I need to notify main thread about finishing, interruption or exceptions in worker thread. 我需要通知主线程有关工作线程中的完成,中断或异常。 Main thread should react on this events, and of course it should not be blocked. 主线程应对此事件做出反应,当然不应阻止它。 As I know it behavior released in android in handler class. 我知道它在处理程序类中的android中发布的行为。 But I have no idea, how it possible to do in java. 但我不知道,如何在java中做到这一点。 Could someone help me with this problem? 有人可以帮我解决这个问题吗?

If you have an event loop in your main thread, you can have a queue which it polls to see if any new events it needs to process are waiting. 如果您的主线程中有一个事件循环,您可以拥有一个轮询的队列,以查看它需要处理的任何新事件是否正在等待。 If you are using Swing or Android there will be an event loop already and a method you call to pass tasks to it. 如果您使用的是Swing或Android,则会有一个事件循环以及您调用以将任务传递给它的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM