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.

}