Wednesday 15 January 2014

When to use shutdown() and when shutdownNow() in ExecutorService?

If your task is non-interruptible use shutdown as shutdownNow wouldn't help.


In Progress .......

Thursday 7 February 2013

Thread.yield() vs Thread.sleep(0)

1. First major difference is sleep can throw interrupted exception where yield doesn't.
2. Its implementation varies in different JVM implementation. In windows till Java 5 Yield use to call sleep(0) internally but from Java 6 implementation has changed.


Note:
Using Thread.yield() is not an effective strategy as you are increasing OS level calls and making the OS context scheduler do more work - in the worst case this can mean a system gets swamped doing context switching.
There are JVM options to control how yield works, e.g. -XX:+DontYieldALot makes yields get ignored.

Wednesday 28 November 2012

What is Spurious Wakeup while wait() in Threads?


According to Wikipedia article on spurious wakeups has this tidbit:
The pthread_cond_wait() function in Linux is implemented using the futex system call. Each blocking system call on Linux returns abruptly with EINTR when the process receives a signal. ...pthread_cond_wait() can't restart the waiting because it may miss a real wakeup in the little time it was outside the futex system call. This race condition can only be avoided by the caller checking for an invariant. A POSIX signal will therefore generate a spurious wakeup.

For understanding what is spurious wakeup this is java doc comment from Object.java :

     * A thread can also wake up without being notified, interrupted, or
     * timing out, a so-called <i>spurious wakeup</i>.  While this will rarely
     * occur in practice, applications must guard against it by testing for
     * the condition that should have caused the thread to be awakened, and
     * continuing to wait if the condition is not satisfied.  In other words,
     * waits should always occur in loops, like this one:
     * <pre>
     *     synchronized (obj) {
     *         while (&lt;condition does not hold&gt;)
     *             obj.wait(timeout);
     *         ... // Perform action appropriate to condition
     *     }
     * </pre>

Wednesday 16 May 2012

Pause and Resume Thread


If you want a single thread that can do start, pause, resume and stop followings  abstract class you need to extend is tested solution for you. Extend  this class, define what task you want to run in thread and it will give you methods to handle different thread life cycle stages. Please let me know if you find anything wrong :)

IMP NOTE: Remember once you call the shutdown/shutdownNow method on executor, any further attempt to submit task will throw RejectedExecutionException.

1. Pausing Normal Thread.run:


package co.rnd.thread.life;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import com.google.common.util.concurrent.Monitor;

public abstract class PausableTask implements  Runnable{

private ExecutorService executor = Executors.newSingleThreadExecutor();
private Future<?> publisher;
protected volatile int counter;
private void someJob() {
System.out.println("Job Done :- " + counter);

}

abstract void task();

@Override
public void run() {
while(!Thread.currentThread().interrupted()){
task();
}
}

public void start(){
publisher = executor.submit(this);
}

public void pause() {
counter = 100;
publisher.cancel(true);
}

public void resume() {
counter = 200;
start();
}

public void stop() {
counter = 300;
executor.shutdownNow();
}
}

2. Pausing Scheduled Thread:
package co.rnd.thread.life;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public abstract class PausableScheduledTask
{
   private ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
   private volatile boolean isPaused = false;

   abstract void task();

   public void start()
   {

         if(executor == null || executor.isShutdown())
   {
   executor = Executors.newSingleThreadScheduledExecutor();
     final Runnable beeper = new Runnable()
      {
         @Override
         public void run()
         {
            if (!isPaused)
               task();
         }
      };
      executor.scheduleAtFixedRate(beeper, getInitialDelay(), getPeriodDelay(), TimeUnit.SECONDS);
   } else
   {
   resume();
   }

     
   }

   public void pause()
   {
      isPaused = true;
   }

   public void resume()
   {
      isPaused = false;
   }

   public void stop()
   {
      executor.shutdownNow();
      executor = null;
   }

   public long getInitialDelay()
   {
      return 0;
   }

   public long getPeriodDelay()
   {
      return 0;
   }

   public TimeUnit getTimeUnit()
   {
      return TimeUnit.SECONDS;
   }
}