Java Interview : Multithreading Part One

This post is part of the Java Interview notes series.
In Java there are 2 ways to create threads, one way is  to extend Thread class and objects of such a class are threads and run in a execution path of their own, you just override the run() method and call the start() method. Similarly another way to create a thread is implement Runnable interface and provide this reference to a Thread class object and call the start() method.
ThreadFactory interface implements the factory method and gives you a way to get a thread. You need to provide an implementation of the newThread() method which returns a thread reference.You can provide custom behaviour in this factory to get statistics of the number of threads created and even control the number of threads which can be created using such a factory.
Threads have getter/setters to work with thread related information like ID,namepriority and status. Thread have 6 states: NewRunnable , Blocked,WaitingTime Waiting and Terminated.
System.exit() or when all the non-daemon threads are finished with their run methods the application ceases to exist.Threads can be stopped by usinginterrupt() or by using an InterruptedException, to check if the thread is interrupted use isInterrupted().
If you want a thread a thread to leave the CPU for a definite amount of time call the sleep() method with the time as the parameter.This method throws a InterruptedException. One more method available to give up CPU processing is the yield() method but this method just gives the JVM to swap it out as it does not need processing right now, but this is just a request and the JVM might not adhere to this request.
If you want threads to run sequentially or one after another use the join() method, it suspends the execution of the calling thread untill the caller thread is done with its task or completed the run method. This method comes with overloaded versions which take time as parameter.
The Unix concept of daemon services come to the Java world in form of the ability to make a thread a daemon thread, thus this thread runs with a lower priority than the user created threads and are generally used for background services. These threads exit when all the user created threads are completed withg their run() method execution.Use setDaemon() to make a thread a daemon thread.
UncaughtExceptionhandler interface gives us a way to catch uncheckedexceptions thrown by the run() method which we implement, as you know run method does not have “throws” in its signature we can provide custom exception implementation thus prevent program exiting when an unchecked exception is thrown. We need to provide the new ExceptionHandler as the parameter to the setUncaughtExceptionHandler() as a parameter.
As the threads start working their is a possibility that they might corrupt some data, ThreadLocal provides us a way to provide all the threads a their own copies of data using the initialValue() method. Threads need to call the get()method and get their won copies which are manipulated by them alone and thus remain in a uncorrupted form throughout.
ThreadGroup is also a way you can group a set of threads under a nice name and issue orders to start all these threads or end them all. This gives you the power to get statistics like the number of active threads at a time etc using getters provided in this class.

Comments

Popular posts from this blog

Java Interview : Threads

Spring Framework Interview Notes : Part Two Wiring

Card Dealer In Java in Less than 5 minutes