Java Thread Interview Questions and Answers

    6 Votes


10) What is meant by Daemon threads in Java ?

Daemon Thread is different from user created threads. It is created by the JVM for executing internal operations like garbage collection. When a non daemon thread stops execution, JVM exits all the daemon threads currently under operation without waiting for their activities to complete. To make a Daemon thread, use code Thread.setDaemon(true).

11) What is difference between threadObject.run() and threadObject.start() in Java ?

StartRun
When program calls start() method, JVM creates a new Thread is created and code inside run method is executed inside in that thread. In case, we are calling the run() method directly, JVM won't create a new thread. Code specified inside the run method will get execute on current Thread.
Cannot call start() method twice on thread object. Can call run method more than once.


12) Can run() method take arguments ?

No : We can't pass arguments to the run() method, which is  defined inside the Runnable interface.

Yes : We can create an overloaded method of run() with arguments. But when a thread is started, JVM will always call the run method without arguments.


13) Can a class extend Thread class, without overriding run() method? What will happen if I call threadObject.run() ?

Case 1 : If you extend Thread class, it is not mandatory to override run method. But if you want to start over a new thread from there on in the code, then it is mandatory to override run().

Case 2 : If you are implementing Runnable interface, then it is mandatory to override run() so that the program would compile without any errors. It also helps you to start over a new thread from there.


14) Can you explain about join() in Thread class ?

join() causes the main thread to wait until the child thread completes it's execution. It is also a blocking method, which blocks the main thread until thread on which join has called die or specified waiting time is over.


15) Can you explain about the thread scheduler?

The entire responsibility of maintaining the sequence of execution of threads, where which thread should be given first preference than the other, lies with the thread scheduler. The scheduling depends on the algorithm of the scheduler. Many types of algorithms exist like preemptive and time slicing with round robin etc.


16) Can you explain about context switching in threads ?

  • A context switch is the switching of the CPU from one process/thread to another. 
  • It perceives the threads or tasks as running at the same time.
  • Most context switching is caused by external interrupts.


17) What are the method available in java.lang.object class and java.lang.Thread class ?

Object :

public final void wait() throws Interrupted exception 
public final void notify()
public final void notifyAll()

Thread:

public final void setPriority(int priority) 
public void start()
public void run()
public static void sleep(long millis) throws Interrupted exception
public final void join()
public void interrupt()
public static void yield()
public void resume()


18) Can you give ,real time example of multi-threaded application ?

FTP application : Transfer and download multiple files simultaneously using multi threading.

Page 2 of 4

Popular Videos

communication

How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

Java OOPS Interview Questions and Answers
Java Interview Questions
Top Advanced Java Interview Questions and Answers
Java Collections Interview Questions and Answers