Write a Multi-Threaded Java Program in which 3 threads are generated to proceed following steps.

Write a Multi-Threaded Java Program in which, one Thread Generates Odd Numbers and Write to a Pipe and the Second Thread Generates Even Numbers and Write to Another Pipe, and a Third Thread Receives the Numbers from both the Pipes and Evaluates if the sum is Multiples of 5. In Unix, a pipe (“|”) operator … Read more

Can you Write a Program with 2 Threads, in which one Prints Odd Numbers and the other Prints even Numbers up to

In Java, you can use wait(  ) and notifyAll(  ) to communicate between threads. The code below demonstrates that:   Note: This a typical example of splitting tasks among threads. A method calls notify/notifyAll( ) as the last thing it does (besides return). Since the printOdd( ) and printEven( ) methods were void, the notifyAll( ) was the last statement. If … Read more

What happens if you Restart a Thread that has Already Started

Get the following exception: Exception in thread “main” java.lang.IllegalThreadStateException at java.lang.Thread.start(Thread.java:595) at deadlock.DeadlockTest.main(DeadlockTest.java:38) If you attempt to restart a thread in Java that has already been started and is still running, it will result in an IllegalThreadStateException. This exception is thrown to indicate that a thread operation is not permitted in its current state. Once … Read more

Explain how you would get a Thread Deadlock with a Code Example

The example below causesa deadlocksituation bythread-1 waiting for lock2and thread-0 waiting for lock1.   package deadlock;   public class DeadlockTest extends Thread { public static Object lock1 = new Object(); public static Object lock2 = new Object(); public void method1() { synchronized (lock1) { delay(500);  //some operation System.out.println(“method1: ” + Thread.currentThread().getName()); synchronized (lock2) { System.out.println(“method1 … Read more

What is the Difference Between Synchronized Method and Synchronized Block?

How does Thread Synchronization Occurs Inside a Monitor? What Levels of Synchronization can you Apply?What is the Difference Between Synchronized Method and Synchronized Block. In Java programming, each object has a lock. A thread can acquire the lock for an object by using the synchronized keyword. The synchronized keyword can be applied in method level … Read more