gpt4 book ai didi

java - 如何无限期地暂停Java中的线程并稍后恢复它?

转载 作者:IT老高 更新时间:2023-10-28 20:54:10 25 4
gpt4 key购买 nike

也许这个问题以前被问过很多次,但我从来没有找到满意的答案。

问题:


I have to simulate a process scheduler, using the round robin strategy. I'm using threads to simulate processes and multiprogramming; everything works fine with the JVM managing the threads. But the thing is that now I want to have control of all the threads so that I can run each thread alone by a certain quantum (or time), just like real OS processes schedulers.

我想做什么:

I want have a list of all threads, as I iterate the list I want to execute each thread for their corresponding quantum, but as soon the time's up I want to pause that thread indefinitely until all threads in the list are executed and then when I reach the same thread again resume it and so on.

问题:

So is their a way, without using deprecated methods stop(), suspend(), or resume(), to have this control over threads?

最佳答案

是的,有:

Object.wait()、Object.notify() 和 java.util.concurrent 中的一堆其他更好的同步原语。

关于java - 如何无限期地暂停Java中的线程并稍后恢复它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2622804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com