如何唤醒“等待”线?
我正在制作一个应用程序,旨在模拟数据包如何通过互联网传输,因此一些数据包会丢失。
主线程等待响应,但是必须有一个超时。
我想要一个在线程中运行的计时器,如果计时器超时,它会中断主线程。我想知道是否可以让两个进程同时运行,即不让一个进程休眠。
I am making an app that is intended to emulate how packets are transmitted over the internet, and so some packets get lost.
The main thread waits for the response, however there has to be a timeout.
I want to have have a timer running in a thread, which interrupts the main thread if the timer times out. I am wondering if you can have both processes running concurrently, i.e. without making one sleep.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
java中存在线程的根本原因是它们可以并行运行。是的,两个线程可以并行运行。对于您的程序,它们是并行运行的。对于 CPU,它们可能会也可能不会,具体取决于您拥有的内核数量。
The very reason for threads to exist in java is so that they can run in parallel. Yes, two threads can run in parallel. For your program they are running in parallel. for CPU they may or may not depending on number of Cores you have.