Sunday, 15 January 2017

Difference in yield and join ?

Difference in yield and join

yield() method pauses the currently executing thread temporarily for giving a chance to the remaining waiting threads of the same priority to execute. If there is no waiting thread or all the waiting threads have a lower priority then the same thread will continue its execution.

join() If any executing thread t1 calls join() on t2 i.e.; t2.join() immediately t1 will enter into waiting state until t2 completes its execution.

Which is more preferred – Synchronized method or synchronized block?

Synchronized block is more preferred way because it doesn’t lock the Object, synchronized methods lock the Object and if there are multiple synchronization blocks in the class, even though they are not related, it will stop them from execution and put them in wait state to get the lock on Object.

What is the difference when the synchronized keyword is applied to a static method or to a non static method?

When a synch non static method is called a lock is obtained on the object. When a synch static method is called a lock is obtained on the class and not on the object. The lock on the object and the lock on the class don’t interfere with each other. It means, a thread accessing a synch non static method, and then the other thread can access the synch static method at the same time but can’t access the synch non static method.

What are the two ways of creating thread?

There are two ways to create a new thread.
Extend the Thread class and override the run() method in your class. Create an instance of the subclass and invoke the start() method on it, which will create a new thread of execution. 

e.g.
public class NewThread extends Thread {
public void run() {
// the code that has to be executed in a separate new thread goes here
}

public static void main(String[] args) {
NewThread c = new NewThread();
c.start();
}
}
Implements the Runnable interface. The class will have to implement the run() method in the Runnable interface. Create an instance of this class. Pass the reference of this instance to the Thread constructor a new thread of execution will be created. e.g. class
public class NewThread implements Runnable {
public void run() {
// the code that has to be executed in a separate new thread goes here
}

public static void main(String[] args) {
NewThread c = new NewThread();
Thread t = new Thread(c);
t.start();
}
}

Preemptive scheduling vs. time slicing?

In preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. 

In time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. 


The scheduler then determines which task should execute next, based on priority and other factors

What is context-switching in multi-threading?

Context Switching is the process of storing and restoring of CPU state so that Thread execution can be resumed from the same point at a later point of time. Context Switching is the essential feature for multitasking operating system and support for multi-threaded environment.

What is volatile keyword in Java?

When we use volatile keyword with a variable, all the threads read its value directly from the memory and doesn’t cache it. This makes sure that the value read is the same as in the memory.