Java Multi-Threading Intrview Questions-1

 Q1. How Java Thread is different than Regular Java Objects?

Ans: Java Thread are same as any regular Java Objects. They are instance of java.lang.Thread class.  However, they have extra capabilities, whatever you written in run() method of Thread class instance will be executed in a separate thread.

Q2. What are all the ways by which a new Thread can be created?

Ans: There are mainly two ways to create new Thread. However, both method requires Thread class.

Approach 1: Using Thread Class, Create an Instance of Thread Class

Thread threadInstance = new Thread(); // Create Instance of a thread class

threadInstance.start(); // Starting thread

Approach 2: Using Runnable interface:  In this approach we need to Implement Runnable interface.

public class TestRunnable implements Runnable {

public void run(){}

}

Thread threadInstance = new Thread(new TestRunnable() ); // Create Instance of a thread class

threadInstance.start(); // Starting thread, it will execute run method of TestRunnable object

Q3. When we should use, approach in which we are sub-classing the Thread (Important)?

Ans: There is no particular rule for defining which approach is better.  However, it is recommended when you need long running threads e.g. ThreadPool (Having like 10 of threads, pre-created). We should use Thread sub-classing approach. Hence, we have 10 long running threads and their responsibility is to run the task submitted to them.

Let’s take an example:  You are having a 10,000 small test files. Now you want to calculate number of words in each file. In this case, what we are going to do, create 10,000 tasks (not 10000 threads). We will submit these 10,000 tasks to ThreadPool (which has 10 threads). Each thread will run 1000 tasks. Hence, using only 10 threads we have completed our entire word count of 10,000 files. These types of BigData Problems can be easily solved using Hadoop Framework (visit: www.HadoopExam.com

Q4. When should we use approach for Runnable interface?

Ans: As mentioned in above question, we should use Thread sub-classing approach, when we need ThreadPool (Long running Threads). And the tasks mentioned in above example should be Runnable. Hence, we will create 10,000 Runnable instances and submitting them to ThreadPool, and each thread from ThreadPool will execute the Runnable instance.

Q5. What happen if we call run() method directly on thread class instead of calling start() method on thread class instance?

Ans: Below code shows, how to start a new thread

Thread threadInstance = new Thread(); // Create Instance of a thread class

threadInstance.start(); // Starting thread

Now if we call threadInstance.run() directly, than it will not create a new Thread and execution will be part of same thread which called run() method. Hence, it is must to call start() method.

Q6.  How to assign name to a Thread and what is the use of it?

Ans: Assigning name to thread will help us debugging multithreaded application. When we see logs, we can easily find which thread is processing our requests and troubleshoot accordingly.

You can assign name to thread as following

·         While creating thread instance ,we can give name to Thread

        Thread thread = new Thread(“HadoopExam.com”); //HadoopExam.com is name of the thread

·        In case of Runnable, we can assign name to Thread as below

TestRunnable runnable = new TestRunnable();

//Name of the Thread is HadoopExam.com and

//runnable is a task, which will be eacuted by threadInstance Thread

Thread threadInstance=new Thread(runnable,”HadoopExam.com”);

To retrieve thread name. we can use following method.

Thread.currentThred.getName();


Q7: How to get handle/refrence of the currently Running thread?

Ans: We can use Thread.currentThread() methods, which will return reference to currently running thread.

Q8. When you start your applications using main() method, in which thread this applications starts?

Ans: Java application will always have at least one thread by default that is called main thread. If you do not have any explicit thread than still application will have one thread that is called main thread.

Q9. In what order threads will be executed?

Ans:  There is no default order by which thread can be executed. It depends on operating system. However, we can control thread ordering multiple ways e.g. Priority, Synchronizing etc.

Q10. What is a “Critical Section” of code?

Ans: In a multithreaded application a code section which is executed by multiple threads are called Critical Section, if Sequence of execution have side effects on final results. For example

public int foo(){

  return i=i+1; //This line is critical section, if used in multithread application.

}

 






http://www.hadoopexam.com/cloudera_certification/cca175/cca_175_hadoop_cloudera_spark_certification_questions_dumps_practice_test.html