+91 90691 39140 | +1 253 214 3115 | info@hub4tech.com | hub4tech

Java Threading Interview Questions and Answers


1
What is a thread? What are the advantages we derived by programming with thread?

Threads allow programs to execute simultaneously. A thread is an independent path of execution in a program. These threads can be executed synchronously or asynchronously. All threads have a priority attached. Thread with a higher priority is executed first.

Advantages:

  • Allows multiple programs to be executed concurrently
  • Cost of thread is low compared to processes in terms of space and communication.
  • Threads are lightweight.
2
Difference between Thread and Process in Java?

Thread is subset of Process, in other words one process can contain multiple threads. Two process runs on different memory space, but all threads share same memory space. Don't confuse this with stack memory, which is different for different thread and used to store local data to that thread.

3
Explain the term thread safety and synchronization.

The term Thread safety means each method in a multithreaded environment doesn’t access data by multiple threads at the same time. Synchronization is the way to provide a secured environment and thus ensure Thread Safety.

4
What is blocking method in Java?

A blocking method is a method which blocks until task is done, for example accept() method of ServerSocket blocks until a client is connected. here blocking means control will not return to caller until task is finished. On the other hand there are asynchronous or non-blocking method which returns even before task is finished.

5
Explain how to create a thread and start it running.

Creating a thread:

Declare a class as a sub class of Thread
Class SampleThread extends Thread
{
Long minSample
{
SampleThread( Long minSample);
}
Public void run()
{
Program goes here
}
}

Run the thread: An instance is created to start the thread.
SampleThread s = new SampleThread(100);
s.start();

Copyright ©2015 Hub4Tech.com, All Rights Reserved. Hub4Tech™ is registered trademark of Hub4tech Portal Services Pvt. Ltd.
All trademarks and logos appearing on this website are the property of their respective owners.
FOLLOW US