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 stat开发者_StackOverflowe. 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.
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).
精彩评论