jeudi 16 mai 2019

Pattern name for update task

I often have following problem to solve: a long running task depends on a bunch of data. The data is updated occasionally and the task must started, the data might be updated meanwhile. Then the task must start over again to update itself. How is this pattern called in programming and is there a structured helper for this maybe in Guava?

Programmatically I would do it like this (omitted background running details):

   AtomicBoolean iAmWorking = new AtomicBoolean(false);
   AtomicBoolean dataIsDirty = new AtomicBoolean(false);
   public void update() {
       dataIsDirty.set(true);
       if(!iAmWorking.compareAndSet(false, true)) {
           return;
       }
       while(dataIsDirty.compareAndSet(true, false)) {
           doSomethingLong();
       }
       iAmWorking.set(false);
    }

Aucun commentaire:

Enregistrer un commentaire