Threading Interview questions And Answers for freshers as well 1 to 3 year experienced so Why threads?
– Need to handle concurrent processes
Definition
– Single sequential flow of control within a program
– For simplicity, think of threads as processes executed by
a program
– Example:
Operating System
HotJava web browser
Questions : 1 | What’s difference between thread and process? |
Answers : 1 |
A thread is a path of execution that run on CPU, a process is a collection of threads that share the same virtual memory. A process has at least one thread of execution, and a thread always run in a process context. A process is a collection of virtual memory space, code, data, and system resources. A thread is code that is to be serially executed within a process. A processor executes threads, not processes. One more significant difference between process and thread is every process has its own data memory location but all related threads can share same data memory and have their own individual stacks. Thread is a light weighted process, collection of threads become process. |
Questions : 2 | What is thread safety and synchronization ? |
Answers : 2 |
Thread safe is used to describe a method that can run safely in multithreaded environments allowing accessing to data in a safe and efficient way. Thread safety is achieved by synchronization. Synchronization assures that the object data is not accessed by multiple threads at the same time. |
Questions : 3 | What is semaphore ? |
Answers : 3 |
Semaphore is an object which helps two threads to communicate with one another in order to synchronize there operation. |
Questions : 4 | What are monitors ? |
Answers : 4 |
Monitor is a body of code which can be executed by only one thread at a time. If any other thread tries to get access at the same time, it will be suspended until the current thread releases the Monitor. |
Questions : 5 | How do we create threads ? |
Answers : 5 |
There are two ways to create a thread. |
Questions : 6 | what’s the difference in using runnable and extends in threads? |
Answers : 6 |
Main differences between threads using runnable interface and by
extending the thread class are |
Questions : 7 | explain Thread.sleep? |
Answers : 7 |
Thread.sleep() is a static method of the Thread class that causes the currently executing thread to delay for a specified number of milliseconds. |
Questions : 8 | How to stop a thread ? |
Answers : 8 |
By use of thread.stop; |
Questions : 9 | What is wait() and notify() ? |
Answers : 9 |
By using wait() and notify(), a thread can give up its hold on a lock at an arbitrary point, and then wait for another thread to give it back before continuing. By executing wait() from a synchronized block, a thread gives up its hold on the lock and goes to sleep.Later, when the necessary event happens, the thread that is running it calls notify() from a block synchronized on the same object. Now the first thread wakes up and begins trying to acquire the lock again. |
Questions : 10 | explain Yielding in threading? |
Answers : 10 |
It causes the currently executing thread object to temporarily pause and allow other threads to execute. |
Questions : 11 | what are daemon threads ? |
Answers : 11 |
Daemon threads are designed run in background. One example of a daemon thread is the garbage collector thread. You can use setDaemon() to mark a thread as daemon. |
Questions : 12 | explain how Scheduling and Priority works in threads? |
Answers : 12 |
Every thread has priority.If, at any time, a thread of a higher priority than the current thread becomes runnable, it preempts the lower priority thread and begins executing. By default, threads at the same priority are scheduled round robin, which means once a thread starts to run |