public class ThreadState extends Thread { public void run(){ System.out.println("I am in Run"); } public static void main(String[] args) throws Exception{ ThreadState threadState = new ThreadState(); threadState.start(); System.out.println(threadState.getStackTrace()); Thread.sleep(100); System.out.println(threadState.getStackTrace()); threadState.start(); System.out.println(threadState.getStackTrace()); } }
class CustomExceptionhandler implements UncaughtExceptionHandler{ public void uncaughtException(Thread paramThread, Throwable paramThrowable) { System.out.println("I have reached the Re Instantiation Of Thread"); new PollingThread().start(); System.out.println("Re Instantiation Of Thread completed and Thread started"); } }
public class PollingThread extends Thread { public void run(){ this.setUncaughtExceptionHandler(new CustomExceptionhandler()); int randomIntvalue = new Random().nextInt(5) ; if(randomIntvalue > 3){ System.out.println("Polling successfull...Int value = "+randomIntvalue); System.out.println("Polling state achieved"); }else{ System.out.println("Polling un- successfull...Int value = "+randomIntvalue); System.out.println("Application needs to Repoll"); intdivideByZero = 1/0; } } public static void main(String[] args){ System.out.println("I am starting to Poll now"); new PollingThread().start(); } }
The above application could go out of memory if the randomIntvalue variable never achieves its desired value. Or in logical terms, the poll never gets over. So this thought could also be presented with a ThreadPool. I have built a Thread Pool of capacity 10 using java.util.concurrent.ExecutorService. If the thread does not have an explicit uncaught exception handler set, and the thread's thread group (including parent thread groups) does not specialize its uncaughtException method, then the default handler'suncaughtExceptionmethod will be invoked.By setting the default uncaught exception handler, an applicationcan change the way in which uncaught exceptions are handled (such as logging to a specific device, or file) for those threads that wouldalready accept whatever behavior the system provided.
class CustomExceptionHandler implements UncaughtExceptionHandler{ public void uncaughtException(Thread paramThread, Throwable paramThrowable) { System.out.println("I have reached the Re Instantiation Of Thread"); PollingThread.submitThreadToPool(new PollingThread()); System.out.println("Re Instantiation Of Thread completed and Thread started"); } } public class PollingThread extends Thread { static ExecutorService executorService = Executors.newFixedThreadPool(10); public void run(){ this.setUncaughtExceptionHandler(new CustomExceptionHandler()); int randomIntvalue = new Random().nextInt(5) ; if(randomIntvalue > 3){ System.out.println("Polling successfull...Int value = "+randomIntvalue); System.out.println("Polling state achieved"); getThreadPool().shutdown(); }else{ System.out.println("Polling un- successfull...Int value = "+randomIntvalue); System.out.println("Application needs to Repoll"); intdivideByZero = 1/0; } } public static void main(String[] args){ Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler()); System.out.println("I am starting to Poll now"); executorService.execute(new PollingThread()); } public static void submitThreadToPool(Runnable runnable){ executorService.execute(runnable); } public static ExecutorService getThreadPool(){ returnexecutorService; } }
Komal Ahluwalia - Linkedin, Google Plus
She is a freelance writer, loves to explore latest features in Java technology.
No comments:
Post a Comment