Skip to main content

Posts

Multi-threading Interview Questions in Java part- 2

Multi-threading 1.What are the minimum requirements for a Deadlock situation in a program?                       For a deadlock to occur following are the minimum requirements: Mutual exclusion : There has to be a resource that can be accessed by only one thread at any point of time. Resource holding : One thread locks one resource and holds it, and at the same time it tries to acquire lock on another mutually exclusive resource. No preemption : There is no pre-emption mechanism by which resource held by a thread can be freed after a specific period of time. Circular wait : There can be a scenario in which two or more threads lock one resource each and they wait for each other’s resource to get free. This causes circular wait among threads for same set of resources. 2. How can we prevent a Deadlock?                       To prevent a Deadlock from occurring at least one requirement for a deadlock has to be removed: Mutual exclusion : We can use optimistic locking to

Multi-threading Interview Questions in Java

Multi-threading 1. What is a Thread in Java? A thread in Java is a lightweight process that runs within another process or thread. It is an independent path of execution in an application. JVM gives each thread its own method-call stack. When we start JVM, Java starts one thread. This thread calls the main method of the class passed in argument to java call. 2. What is the priority of a Thread and how it is used in scheduling? In Java, every Thread has a priority. This priority is specified as a number between 1 to 10. Scheduler in Java schedules different threads based on the priority of a thread. It is also known as pre-emptive scheduling. The thread with higher priority gets preference in execution over a thread with lower priority. 3. What is the default priority of a thread in Java? In Java, a new thread gets the same priority as the priority of the parent thread that creates it. Default priority of a thread is 5 (NORM_PRIORITY). 4. What are the three