Wednesday 15 July 2015

Implement thread in singleton class

Create thread for Singleton class

If we create thread of singleton object by extending the Thread class, there will IllegalThreadStateException.

This exception is because we cannot call the start method on same instance twice.

But when create threads for an class using Runnable interface, the class object will sharable among different threads and there will no IllegalThreadStateException.

By extending Thread class

class SingletonThread extends Thread {
       private SingletonThread() {

       }
       private static volatile SingletonThread INSTANCE;
       private int instVar = 0;

       public static SingletonThread getInstance() {
              Object mutex = new Object();
              if(null == INSTANCE) {
                     synchronized(mutex) {
                           INSTANCE = new SingletonThread();
                     }
              }
              return INSTANCE;
       }

       @Override
       public void run() {
              for (int i = 0; i <3; i++) {
                     System.out.println(i":Thread variable " + instVar++ );
              }
       }
}

public class TestImplemetation {
       public static void main(String[] argsthrows InterruptedException {

              // Here we are using thread class
              Thread t3 = SingletonThread.getInstance();
              Thread t4 = SingletonThread.getInstance();
              t3.start();

              /**
               * t3 and t4 are same instance.
               * java.lang.IllegalThreadStateException
               * Second time start called on same instance.
               */
              t4.start();
       }
}

Output:
0:Thread variable 0
1:Thread variable 1
2:Thread variable 2
Exception in thread "main"
java.lang.IllegalThreadStateException
       at java.lang.Thread.start(Unknown Source)
       at core.java.thread.TestImplemetation.main(TestImplemetation.java:81)

By implementing the Runnable interface

class SingletonRunnable implements Runnable {
       private SingletonRunnable() {

       }

       private static volatile SingletonRunnable INSTANCE;

       private int instVar = 0;

       public static SingletonRunnable getInstance() {
              Object mutex = new Object();
              if(null == INSTANCE) {
                     synchronized(mutex) {
                           INSTANCE = new SingletonRunnable();
                     }
              }
              return INSTANCE;
       }

       @Override
       public void run() {
              for (int i = 0; i < 3; i++) {
                     System.out.println(i":Runnable variable "+instVar++ );
              }
       }
}

public class TestImplemetation {
       public static void main(String[] argsthrows InterruptedException {

              SingletonRunnable runnable1 = SingletonRunnable.getInstance();
              SingletonRunnable runnable2 = SingletonRunnable.getInstance();

              /**
               * runnable1 and runnable2 are same instance which are shared for
               * two thread. so there is no IllegalThreadStateException.
               * */
              Thread t1 = new Thread(runnable1);
              Thread t2 = new Thread(runnable2);
              t1.start();
              t2.start();
       }
}

Output:
0:Runnable variable 0
1:Runnable variable 1
2:Runnable variable 2
0:Runnable variable 3
1:Runnable variable 4
2:Runnable variable 5

1 comment:

  1. Initializing two threads with the same instance of a runnable

    It's absolutely fine to do it so long as the code you're running is designed to support that. Not only will it save some memory by having a single instance instead of multiple instances, but if those threads are trying to communicate via shared data, then it may be absolutely required!

    Admittedly communicating via shared state is where threading often gets tricky, so this needs to be done carefully, but from the point of view of the threading system itself, there's absolutely no problem in having two threads call the run method of a single Runnable instance.

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...