模拟作业检查点java

发布于 2024-10-03 00:21:57 字数 539 浏览 1 评论 0原文

我正在尝试用java模拟网格计算的作业检查点。我有两个属于线程的类 Job 和 CheckPointInterrupter。 CheckPointInterrupter 类定期获取作业的检查点。

为了使模拟简单,我只是将 Job 的变量值作为检查点状态。 我面临着关于 Job 类的设计的困境。由于 suspend() 和resume() 方法现已弃用,我不希望 CheckPointInterrupter 在作业中使用它们。所以我寻找替代方案并找到 http://www.java2s.com/Code/ Java/Threads/Anotherwaytosuspendandresume.htm

如果这是要走的路,那么我必须将检查点相关代码引入到作业类中。这是否违背了类具有单一职责的原则?另外,有人可以告诉我网格/分布式环境中工作的性质吗?这些工作会让自己成为“检查点”吗?任何建议/指向资源/谷歌搜索词将不胜感激。谢谢。

I am trying to simulate job checkpointing for grid computing in java. I am having two classes Job and CheckPointInterrupter that are threads. The CheckPointInterrupter Class takes checkpoints of a job at regular intervals.

To keep the simulation simple, I am just taking the values of the variables of Job as the checkpoint state.
I am facing dilemma regarding the design of the Job class. As the suspend() and resume() methods are now deprecated, I don't want the CheckPointInterrupter using them on the Job. So I looked for alternatives and found http://www.java2s.com/Code/Java/Threads/Anotherwaytosuspendandresume.htm

If that is the way to go, then I'd have to introduce checkpointing related code into the Job Class. Wouldn't that go against the principle of a Class having a single responsibility?. Also, Could somebody enlighten me the nature of jobs in a grid/Distributed environment?. Would these jobs make themselves "checkpointable"?. Any advice/pointing to resource/google search term would be much appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同尘 2024-10-10 00:21:57

在不知道您正在实施哪种“网格计算”的情况下,您唯一的机会是让“Worker”方法意识到它的可中断性。基本上这就是 java 中中断的工作方式。

正如您所看到的,stop()suspend()resume() 已被弃用,因为它们可能会导致死锁。
时间
现在中断线程的唯一方法是使用 Thread.interrupt() 并让“worker”方法通过定期查询 Thread.interrupted() 来处理中断。

另一种方法可能是实现一些其他协议来通知工作线程正在暂停/恢复/无论什么,但关键点是,工作方法必须意识到这一点。

你是对的,这违反了单一关注原则,但在实践中,你可以将几乎所有这些逻辑提取到专门为此设计的类中(例如某种“SuspensionHandler”,“WorkContext”等)

但是,这这是一个非常复杂的领域,有很多书籍可以提供很好的建议。 (我非常喜欢Java并发实践,如果我没记错的话,那里有一个类似的例子)。

Without knowing what kind of "grid-computing" you are implementing, your only chance is to have the "Worker" method to be aware of it's interruptibility. Basically that is how interrupting generally works in java.

As you have seen correctly, stop(), suspend(), resume() have been deprecated, because they could cause deadlock.
T
he only method to interrupt a thread now is to use Thread.interrupt() and let the "worker"-method handle the interruption by regularly querying Thread.interrupted().

An alternative could be to implement some other protocol to inform the worker thread of being paused/resumed/whatever, but the key-point is, the worker-method has to be aware of that.

You are right that this violates the single-concern principle, but in practice, you can extract nearly all of this logic into classes that are specifically designed for this (E.g. some kind of "SuspensionHandler", "WorkContext" etc.)

However, this is a very complex area and there are a lot of books out there that give good advice. (I like Java Concurrency in Practice very much, and if I remember correctly, there is a similar example in there).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文