Question 1: What Is Meant By A Thread In Java?

A Thread refers to an individual path of execution. This would generally refer to a way or process of taking advantage of multiple CPU available in a working machine. By simply employing a thread, a user could be able to speed up the CPU bound task. For example, if a thread takes a time of 100 milliseconds to complete a particular task, then the user could eventually make use of 10 threads to reduce the time taken to complete the task in 10 milliseconds. The JAVA provides an excellent for this multi-thread process at language levels and now this has become a very strong selling point.

Question 2: Difference between Java Thread and Process?

Thread is the subset of Process. The Process could contain multiple threads. In terms of processing, two similar or dynamic tasks or process would run on different memory spaces, but all the threads would share the same memory space.

Question 3: How to Implement a Thread in Java?

On language level, there are 2 ways to implement a Thread in Java. An instance of java.lang.thread would represent a thread, but it would eventually need a task to execute. This task would or must be an instance of interface java.lang.Runnable. The Thread class itself implements a Runnable, and so, the user could be able to override the run ( ) method. This could be done either by extending the Thread class or just by implementing the Runnable interface.

Question 4: When to Use the Runnable Vs Thread in Java?

The Java programming language does not support multiple inheritance of class, but, it would allow the user to implement multiple interfaces. So, the users could better implement Runnable and could then extend the Thread, if there is a need for extending another class.

Question 5: What Is the Difference Between The start ( ) and run ( ) Method of Thread Class?

In the Java threading model, the start ( ) method would be used to start a newly created thread and this start ( ) would internally call run ( ) method. Here a difference calling run ( ) method would be directly invoked. If the user invokes the run ( ) as a normal method, then it would call the same thread and no new thread would get started. This is would be the final case when the user calls start ( ) method.

Question 6: What Is The Difference Between Java Runnable And Callable?

Both Runnable and Callable would represent the task that is intended to be executed in a separate thread. Runnable would be provided to be available there in the JDK 1.0, whereas the Callable would be added in JDK 1.5. Here, the main difference between these would be, Callable’s call ( ) method returns values and throws an Exception which would not be possible with the Runnable’s run ( ) method. Callable would also return Future Object that could hold the result of the computation.

Question 7: Difference between CyclicBarrier and CountDownLatch in Java?

A CountDownLatch would count to Zero, and once the count got finished, the user could not be able to reuse this CountDownLatch. But, the CyclicBarrier could be used even after it gets broken. Both of these CyclicBarrier and the CountDownLatch waits for a number of threads on one or more events of processing.

Question 8: What Is Meant By Thread Safety?

Thread safety is an initial property of an object or code that guarantees the execution of threads and events that are processed by multi thread in any manner.

Question 9: How to Stop a Thread in Java?

There are several control methods that are initially present to stop the threads in Java. Some of the control methods like, stop ( ), suspend ( ) and resume ( ) are the basically used control methods to deprecate the potential deadlock threats. But to manually stop the threads, the programmers would either take the advantage of volatile Boolean variable and they check all the iterations, if, the run method has loops or interrupt threads to cancel the tasks abruptly.

Question 10: What Happens In A Thread, When An Exception Occurs?

If an Exception or an Un-handled exception has been caught in a thread, then the thread would eventually get a call back. In this case, the Java Virtual Machine will query the thread for its UncaughtExceptionHandler using Thread.getUncaughtExceptionHandler ( ) and this will invoke the handler’s UncaughtException ( ) method that passes the thread and execution as arguments.

Question 11: How to Share Data between 2 Threads in Java?

By using the shared object or by using the concurrent data structure like BlockingQueue, one could easily be able to share data between 2 threads. This process implements a Producer Consumer pattern that uses wait and notify methods to get involved in sharing objects between 2 threads without any ease.

Question 12: Reasons For Calling Wait And Notify Method From Synchronized Block?

The main reasons for calling wait and notify method from synchronized block is that these methods are made to be called from synchronized block mandatorily by the Java API. If the method is not called from the specified synchronized context, then the code will throw IllegalMonitorStateException. Other subtle reasons for calling wait and notify method from the synchronized block would be to avoid the race condition.

Question 13: What Is Meant By Busy Spin?

Busy Spin is a general technique that would be employed by the concurrent programmers to initiate and make a thread wait on certain conditions. This method would not relinquish CPU. This would just run the empty loop.

Question 14: Which JVM Parameter Is Used To Control Thread Stack Size?

By default, the –Xss parameter would be used to control the stack size of a thread in Java.

Question 15: How to Force Start a Thread in Java?

In Java, the thread scheduler would control the threads and the Java would not expose any API to control the thread schedule. So, there is no way to force start a thread in Java. Only the user could be able to make requests using System.gc ( ), but still this would not provide any guarantee.