5 Replies Latest reply: Aug 3, 2011 1:48 AM by 836548 RSS

    is there an API to serialize thread state ?

    813175
      Hello,
      I am starting some threads using Executors and I need a system to restart a given thread if the JVM was shut down before the thread completed (power outage ...).

      What I could implement manually is for example a thread manager:

      - Start thread id 1
      - Write to a file the thread id + the required information to eventually restart it
      - Put in a list of running threads the id 1.

      When thread finishes:
      - remove the file
      - remove it from the running thread list

      During the startup of my program, the thread manager would have a look if there are some thread files on the file system = thread did not complete when the program was shut down. In that case, restart the thread using the files.

      Isn't there already an API which would allow me to do something like this ?

      regards,
      Jesse
        • 1. Re: is there an API to serialize thread state ?
          EJP
          You will have to forget about the thread and concentrate on transactionalizing what it is doing. So that it can be resumed from the last commit.
          • 2. Re: is there an API to serialize thread state ?
            813175
            EJP wrote:
            You will have to forget about the thread and concentrate on transactionalizing what it is doing. So that it can be resumed from the last commit.
            The objects that are processed by the threads do already have a persistent status: "pending", "done", "error" ... The "pending" status is set when the thread starts. When it ends it set the object status to "done". The problem is that if I have a given object with status "pending" I cannot say if there is actually a thread processing it or not.
            • 3. Re: is there an API to serialize thread state ?
              802316
              Why not have a state like "processing" which is set when the task is started to say when processing has started.
              • 4. Re: is there an API to serialize thread state ?
                813175
                Peter Lawrey wrote:
                Why not have a state like "processing" which is set when the task is started to say when processing has started.
                That's exactly my "pending" status. But if the program crashes or is restarted just after some objets have been set to pending (=processing), they are in this status even if there are not being processed.

                Maybe I could just restart the processing of all object which have the "pending" status.
                • 5. Re: is there an API to serialize thread state ?
                  836548
                  JesseJames wrote:
                  Peter Lawrey wrote:
                  Why not have a state like "processing" which is set when the task is started to say when processing has started.
                  That's exactly my "pending" status. But if the program crashes or is restarted just after some objets have been set to pending (=processing), they are in this status even if there are not being processed.

                  Maybe I could just restart the processing of all object which have the "pending" status.
                  You could do so, but there is a possibility of picking the same record twice for processing . I would suggest to change the status from pending to 'processing' as soon as it gets picked.

                  If system crashes or restarted then you could pick/consider the records with status as processing as picked but not completed due to system restart/crash.