Core Java Interview Questions-Multithreading

This Core Java Interview Questions-Multi threading page provides all the interview questions about java multithreading concept including multitasking, multithreading, how to create a thread, run method, runnable interface, thread identities, daemon threads, non-daemon threads, thread synchronization and many more questions with clear answers.


What is Multitasking?
Performing more than more than task simultaneously.


What is the difference between Multi-tasking and Multi-Threading?
In Multitasking, n tasks execute inside n process spaces.
In Multithreading, n tasks(threads) execute inside a single process space.


How can we create Thread?
We can create thread either by
1) extending Thread class or
2) implementing Runnable interface.


Does pre-defined run() contains any logic?
No, Predefined Thread class having run() method with the empty definition.


When we use Runnable interface to create Thread?
If we want to apply the Thread functionality only to Child class in Is-A relation.


What are the specifications of Runnable interface?
The runnable interface is having only one specification(abstract method) that is run() method.


What is the Runnable state?
When we start the thread, it will move into waiting state(queue state) is called Runnable state.


What is waiting-state in thread life cycle?
Whil the thread is executing, it will fall into waiting-state.
for example,
1) To read input
2) Wait for resource
3) sleep(), join()….


How to execute thread logic?
We need to invoke the start() method on thread object to execute thread logic.


What is the Single threaded application?
JVM creates and starts a thread when we run java application is called main thread.
if no other thread has created from the main thread is called Single threaded application.


What is Multi-threaded application?
Defining user threads from main thread is called Multi-threaded application in java.


Can we call the run() method directly?
Yes, but it will execute in the current thread space only.
Thread logic executes separately only if we call the start() method.


What is the use of sleep() method?
Causes currently thread execution to sleep(temporarily cease the execution) for specified number of
milliseconds.


Why run() cannot throws InterruptedException?
the run() method is the pre-defined method and it doesn’t throw InterruptedException in its definition.


How to check thread properties such as Name & Priority?
Using getter methods of Thread class
public final int getPriority();
public final String getName();


How to set identity to Thread?
We can set identities to Threads in 2 ways
1) using Thread class constructor
2) using setName() method, we can rename the thread.


What is the use of join() method?
Used to join the thread from the current thread.
Current thread should wait until joined thread moved to the dead state.


Can we call start() method concurrently on the thread?
No, it throws IllegalThreadStateException


What is the Runnable object?
Instantiation of a class which has implemented the Runnable interface is called Runnable object.


Can we start() Runnable object directly?
No, the Runnble object doesn’t contain start() method.


What is the use of Thread priority?
Thread priorities using by OS to schedule the threads when these threads are in waiting-state or in
the runnable state.


How to get current thread instance?
using currentThread() method.
Prototype is…
public static Thread currentThread();


What is Thread synchronization?
Synchronization is the concept of allowing only a single thread while multiple threads are trying to
access a resource.


Can we synchronize a class completely?
No, we can synchronize either block or method.


What is block synchronization?
It is possible to synchronize the only block of instructions instead of complete object(class) functionality.


What is Non-Daemon thread?
The non-daemon thread runs execute foreground logic of the application.
Every thread gets Non-daemon behavior as soon as it has created.


What is Daemon Thread?
Daemon thread runs behind the application and provides service to Non-daemon threads.


How to create User Daemon thread?
Using pre-defined method available in Thread class.
public void setDaemon(boolean on)


Can we change the on-Daemon behavior of main thread to DDaemon?
No, we can’t change the behavior of thread once it has been started.
Main thread run by JVM implicitly when we invoke java application.


>Core Java Interview Questions on Garbage Collection<
‚Äč

Share this