Threads Interview Questions in Java
(1) What is Thread?
Thread is a class.it is a lightweight process for
performing a certain task
(2) Why Thread is light weighted?
Since it shares resources that’s why it is light
weighted.
(3) What is life cycle of Thread?
Born => run => blocked => dead
(4) How two threads communicate with each other?
(5) How two process communicate with each other?
With the help of environment variables
(6) What are Daemon Threads?
Daemon threads are background threads that support the other
threads running in the same process.Ex: garbage collector
(7) What are green threads?
(8) What are native threads?
Threads scheduled by operating system are native
threads.
(9) What is deadlock condition?
Deadlock is condition when two or more threads
waiting for each other to release the lock in a circular chain
(10) How many methods are in Thread class?
start()
sleep()
getPriority()
setPriority()
join()
stop()
yield()
isDaemon()
(11) What is start()?
It is used to start a thread
(12) What is stop()?
It is used to stop a thread
(13) What is sleep()?
It is used for wait the operation for some milliseconds and
nanoseconds.
(14) What is starvation and livelock?
Starvation and livelock both are same.it happens when a
thread wait for a long time for execution.it occurs in non-preemptive
scheduling.
(15) What is method or block synchronization?
(16) What is monitor key?
Monitor key is lock. When one thread occupies the monitor
key than other threads must wait until that thread exit from monitor.
(17) How many monitor keys are assigned for a class?
(a) one for static method
(b) one for instance method
(18) What is synchronization?
It ensure that only one key executes at a time.
(19) What is racing condition?
When two or more threads trying to access the same resource
concurrently is called racing condition.
(20) How do you solve racing condition?
With the help of synchronization
(21) How many types of synchronization?
There are 3 types of synchronization :
(a) block synchronization
(b) method synchronization
(c) static synchronization
(22) In which package thread class is found?
(23) What is work of Daemon thread?
It supports the other main threads. Example System.gc()
(24) What is monitor?
Monitor is object which contains synchronized code in it.
(25) What is process?
Execution of a program is process. it is heavyweight and
contains at least one thread
(26) When daemon threads get dead?
When jvm get stop
(27) What is scheduling?
Scheduling is a process used to determine how threads are
allocated cpu time.
(28) What is preemptive scheduling?
Thread scheduler preempts the running thread and allow
different thread to execute.
(29) What is non-preemptive scheduling?
The thread scheduler never interrupts the running thread
(30) What is time slicing scheduling?
In time slicing scheduling, scheduler allocates a period of
time that each thread can use the CPU after completion of time the thread get
exit.
(31) How many Thread classes are there?
Thread,ThreadGroup,RunTime
(32) Difference between stop() and run()?
In run method, some logical code is present which
determines the operation of thread.
Stop() terminates the running thread.
(33) How do we find that our program is deadlocked?
By tracking thread dump using kill-3, using Jconsole or
visual vm.
(34) What is notify()?
It resumes the thread in running state that is in wait
state.
(35) What is notifyAll()?
It resumes all the threads in running state that are in
wait state.
(36) What is join()?
Calling thread does not release the monitor, for example
t2 calls t1.join() then t2 will wait and T1 does not release the lock
until it gets finished completely.
(37) What is the difference between process and thread?
Process
|
Thread
|
Process contains threads
|
Threads do not contain process
|
Processes are heavy weighted
|
Threads are light weighted
|
Process communicates by environment variables
|
Threads communicate by shared memory area
|
No comments:
Post a Comment