Popular
Interview Question on multithreading
Multithreading is one of the popular topic on interview
question, there are variety of question are asked on multithreading, here I am
adding few important questions.
Print even and
odd number in sequence using 2 threads
package
com.mk.logicaltest.threadingproblem;
import
java.util.concurrent.atomic.AtomicInteger;
public class
EvenOddPrinter {
private static
AtomicInteger atomicInteger = new
AtomicInteger(1);
public static void
main(String[] args) {
Runnable print = ()
-> {
while (atomicInteger.get()
< 10) {
synchronized (atomicInteger) {
if (atomicInteger.get() %
2 == 0 && Thread.currentThread().getName().equals("Even")) {
System.out.println("Even
Thread: " + atomicInteger.getAndIncrement());
} else if (atomicInteger.get() %
2 != 0 && Thread.currentThread().getName().equals("Odd")) {
System.out.println("Odd
Thread: " + atomicInteger.getAndIncrement());
}
}
}
};
Thread evnThread = new Thread(print);
evnThread.setName("Even");
evnThread.start();
Thread oddThread = new Thread(print);
oddThread.setName("Odd");
oddThread.start();
}
}
Create custom
deadlock-
package
com.mk.logicaltest.threadingproblem;
public class
CustomDeadlock {
public static void
main(String[] args) {
String resource1 = "res1";
String resource2 = "res2";
new Thread(()-> {
synchronized (resource1) {
try {
Thread.sleep(10000);
System.out.println("locked
resource1");
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
synchronized (resource2) {
System.out.println("locked
resource2");
}
}
}).start();;
new Thread(()-> {
synchronized (resource2) {
try {
Thread.sleep(10000);
System.out.println("locked
resource2");
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
synchronized (resource1) {
System.out.println("locked
resource1");
}
}
}).start();;
}
}
How
synchronized block acquire locks-
When a synchronized method is called from a thread it needs to acquire
the intrinsic lock. Intrinsic lock is per object level lock. When a thread
acquire a lock on an object than no other thread can access the object lock,
although another thread can access the lock on different object of same class. But
if a thread acquire the class level lock via static block than no other thread can
access the lock of any object of that class.
package
com.mk.logicaltest.threadingproblem;
public class
SynchronizedBlock{
public static void
main(String[] args) {
SynchronizedBlock synchronizedBlock = new
SynchronizedBlock();
Thread th1 = new
Thread(()->{
SynchronizedBlock.staticSync();
});
th1.start();
Thread th2 = new
Thread(()->{
synchronizedBlock.nonStaticSync();
});
th2.start();
}
private static synchronized void
staticSync() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println(e.getLocalizedMessage());
}
System.out.println("Hello
sync static");
}
private synchronized void
nonStaticSync() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(e.getLocalizedMessage());
}
System.out.println("Hello
sync non static");
}
}
No comments:
Post a Comment