Since you are using a Thread, the code further below might give results like:
waiting...One
waiting...Three
waiting...Two
Notified...Two
Notified...Three
Then the code is running until it hits a dead lock. Why is Notified...One missing in the output above? Needs explanation ... (You can get similar result as above when executing following code several time)
class A {
synchronized void waitThread(String threadName) {
System.out.println("waiting..." + threadName);
try {
wait();
} catch(InterruptedException e) { }
System.out.println("Notified..." + threadName);
}
synchronized void notifyThread() {
notifyAll();
}
}
class T1 extends Thread {
A a;
T1(A r, String n) {
a = r;
setName(n);
}
public void run() {
a.waitThread(getName());
}
}
class T2 extends Thread {
A a;
T2(A r, String n) {
a = r;
setName(n);
}
public void run() {
a.notifyThread();
}
}
public class DemoWait {
public static void main(String args[]) {
A a1 = new A();
T1 t1 = new T1(a1,"One");
T1 t2 = new T1(a1,"Two");
T1 t3 = new T1(a1,"Three");
t1.start();
t2.start();
t3.start();
T2 t = new T2(a1,"Four");
t.start();
}
}
You simply have a race condition. It's possible that the thread referenced by the variable t executes notifyAll() before the thread referenced by t1 executes the waitThread(..) method. This is not deadlock. Some of your waits just happen after your notifyAll().
You are facing the problem of Spurious wakeup.So what is happening that the thread which is notifying the all other thread might be call earlier of other thread and after that other thread will run and wait for the wake up.Because of spurious wake up some thread complete.
Change your code...
class A{
boolean flag=true;
synchronized void waitThread(String threadName){
System.out.println("waiting..."+threadName);
try{
while(flag){
wait();
}
}catch(InterruptedException e){ }
System.out.println("Notified..."+threadName);
}
synchronized void notifyThread(){
flag=false;
notifyAll();
} }
Related
I was trying the wait and notify scenario, getting --> Exception in thread "Thread-1" java.lang.IllegalMonitorStateException when calling notify.
wait method releases the lock, so the threadB can execute the and from threadB i'm calling lock.notify for threadA.
Could you help me on this?
class SynchronizedCodee {
int a = 5;
Lock lock = new ReentrantLock();
public void threadA()
{
lock.lock();
try {
lock.wait();
System.out.println("A = "+a);
} catch (Exception e) {
// TODO Auto-generated catch block
// e.printStackTrace();
}
finally
{
lock.unlock();
}
}
public void threadB()
{
if(lock.tryLock())
{
this.a = 11;
System.out.println("B = "+a);
lock.notify(); // getting erro over here
}
else
{
System.out.println("didn't managed to get a lock");
}
}
}
class ThreadA extends Thread{
SynchronizedCodee s;
public ThreadA(SynchronizedCodee s) {
this.s = s;
}
public void run()
{
s.threadA();
}
}
class ThreadB extends Thread{
SynchronizedCodee s;
public ThreadB(SynchronizedCodee s) {
this.s = s;
}
public void run()
{
s.threadB();
}
}
public class SynchronizedCode{
public static void main(String ag[]) throws InterruptedException
{
SynchronizedCodee s = new SynchronizedCodee();
ThreadA t1 = new ThreadA(s);
ThreadB t2 = new ThreadB(s);
t1.start();
Thread.sleep(100);
t2.start();
}
}
You are calling wait and notify on explicit lock objects and that is not legal. If you are using explicit lock objects, you have to use Condition object associated with it. Then you should call condition.await and condition.signalAll methods instead of wait and notify. Here's the idiom for using explicit locks in your particular scenario.
final Condition setA = lock.newCondition();
public void threadA() {
lock.lock();
try {
while (a == 5)
setA.await();
System.out.println("A = " + a);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
lock.unlock();
}
}
public void threadB() {
lock.lock();
try {
this.a = 11;
System.out.println("B = " + a);
setA.signalAll();
} finally {
lock.unlock();
}
}
And this program produces the following output:
B = 11
A = 11
Here is the code snippet:
public class PrintEvenOdd
public static class SynchronizedThreadMonitor {
public final static boolean ODD_TURN = true;
public final static boolean EVEN_TURN = false;
private boolean turn = ODD_TURN;
public synchronized void waitTurn(boolean oldTurn) {
while (turn != oldTurn) {
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException in wait(): " + e);
}
}
}
public synchronized void toggleTurn(){
turn ^= true;
notify();
}
}
public static class OddThread extends Thread {
private final SynchronizedThreadMonitor monitor;
public OddThread(SynchronizedThreadMonitor monitor) {
this.monitor = monitor;
}
#Override
public void run() {
for (int i=1; i<=100; i+=2) {
monitor.waitTurn(SynchronizedThreadMonitor.ODD_TURN);
System.out.println("i= " + i);
monitor.toggleTurn();
}
}
}
public static class EvenThread extends Thread {
private final SynchronizedThreadMonitor monitor;
public EvenThread(SynchronizedThreadMonitor monitor) {
this.monitor = monitor;
}
#Override
public void run() {
for (int i=2; i<=100; i+=2) {
monitor.waitTurn(SynchronizedThreadMonitor.EVEN_TURN);
System.out.println("i= " + i);
monitor.toggleTurn();
}
}
}
public static void main(String[] args) throws InterruptedException {
SynchronizedThreadMonitor monitor = new SynchronizedThreadMonitor();
Thread t1 = new OddThread(monitor);
Thread t2 = new EvenThread(monitor);
t1.start();
t2.start();
t1.join();
t2.join();
}
}
Using 2 threads to print numbers. One prints odd numbers and another prints even numbers.
In my understanding, both waitTurn and toggleTurn share the same LOCK of the instance. So if one holds the LOCK, the other method could not run. So if EvenThread first invokes waitTurn method and wait for the turn change, it holds the LOCK, then OddThread could not enter the toggleTurn method and set the turn. This should lead to a deadlock as per my understanding. But it did not happen.
Can someone please explain why the deadlock did not happen?
"So IF EvenThread first run waitTurn method and wait for the turn change, it holds the LOCK, the OddThread could NOT enter the toggleTurn method"
It holds the LOCK only small period of time, until method wait() is invoked. Method wait() releases the LOCK and allows another thread to enter the critical section.
I have two thread classes: one that prints numbers from 0 to 9, and another from 100 to 109. What I want is to make the first thread wait for the other one to finish. For this, I used the join() method, but it's not working. Please tell me where I'm going wrong:
//demonstrates the use of join() to wait for another thread to finish
class AThread implements Runnable {
Thread t;
AThread() {
t = new Thread(this);
}
public void run() {
try {
for (int i=0; i<10; i++) {
System.out.println(i);
Thread.sleep(10);
}
} catch (InterruptedException e) {
System.out.println(t + " interruped.");
}
}
public void halt(Thread th) {
try {
th.join();
} catch (InterruptedException e) {
System.out.println(t + " interruped.");
}
}
}
//a different thread class (we distinguish threads by their output)
class BThread implements Runnable {
Thread t;
BThread() {
t = new Thread(this);
}
public void run() {
try {
for (int i=100; i<110; i++) {
System.out.println(i);
Thread.sleep(10);
}
} catch (InterruptedException e) {
System.out.println(t + " interruped.");
}
}
}
public class WaitForThread {
public static void main(String[] args) {
AThread t1 = new AThread();
BThread t2 = new BThread();
t1.t.start();
t1.halt(t2.t); //wait for the 100-109 thread to finish
t2.t.start();
}
}
You call join on the thread before it has started. That doesn't work; in that case, join will return immediately, it's not going to wait until the other thread has started and stopped later. You can see this in the API documentation:
Thread.join()
This implementation uses a loop of this.wait calls conditioned on this.isAlive.
Thread.isAlive()
Tests if this thread is alive. A thread is alive if it has been started and has not yet died.
Reorder the statements in your main method
t1.t.start();
t2.t.start();
t1.halt(t2.t); //wait for the 100-109 thread to finish
edit to answer your questions in the comments:
If you want the thread in AThread to wait for the thread in BThread to finish before doing its job, then you'll need to call join in AThread.run, and change your main method:
class AThread implements Runnable {
Thread t;
Thread threadToWaitFor;
AThread(Thread threadToWaitFor) {
t = new Thread(this);
this.threadToWaitFor = threadToWaitFor;
}
public void run() {
// First wait for the other thread to finish
threadToWaitFor.join();
// ...
}
// ...
}
public class WaitForThread {
public static void main(String[] args) {
BThread t2 = new BThread();
AThread t1 = new AThread(t2.t);
t2.t.start();
t1.t.start();
}
}
So, i apologize for the title. It's quite hard to explain in one sentence what i would like to do if you have no idea on how it is called.
So assume i can only use primitive thread functions (wait, notify, no concurrent package)
The program has 3 threads, all of them are the same and are called by the main thread. They behave normally until one of the three get an exception and so it must wait for the end of the remaining 2 threads in order to start a recovery process.
I was thinking about a static variable but I'm not really sure about it, i would love to keep it as simple as possible.
Each thread starts at the same time.
I don't see any reason why you can't use a static variable like you suggest. Here's how I would do it with an inner class...
private static boolean running = true;
public void test26546397() {
while (true) {
Thread t1 = new Thread(new MyRunnable());
Thread t2 = new Thread(new MyRunnable());
Thread t3 = new Thread(new MyRunnable());
t1.start();
t2.start();
t3.start();
try {
t1.join();
t2.join();
t3.join();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
running = true;
// Do recovery
}
}
public class MyRunnable implements Runnable {
#Override
public void run() {
while (running) {
try {
// doStuff
} catch (Exception ex) {
running = false;
}
}
}
}
I would of course replace the while (true) with something a little more suitable.
I think you need java.concurrent.CountdownLatch, however if the java.concurrent package is not available to you can code this yourself using Object.wait/notify and synchronized blocks.
The latch can then be decremented in a finally {} on each Thread, this will be run if the Thread completes, or an exception occurs.
Your main program then just needs to wait for count to become 0.
public class StackOverflow26546397 {
static class CountdownLatch {
private int count;
private Object monitor = new Object();
public CountdownLatch(int count) {
this.count = count;
}
public void countDown() {
synchronized (monitor) {
count--;
monitor.notifyAll();
}
}
public void await() throws InterruptedException {
synchronized (monitor) {
while (count > 0) {
monitor.wait();
}
}
}
}
static class Job implements Runnable {
private CountdownLatch latch;
public Job(CountdownLatch latch) {
this.latch = latch;
}
#Override
public void run() {
try {
// do work.
Thread.sleep((long) (Math.random() * 3000d));
} catch (InterruptedException e) {
//
} finally {
latch.countDown();
}
}
}
public static void main(String[] args) throws InterruptedException {
CountdownLatch latch = new CountdownLatch(3);
new Thread(new Job(latch)).start();
new Thread(new Job(latch)).start();
new Thread(new Job(latch)).start();
latch.await();
System.out.println("All threads finished");
}
}
Not sure what you are trying to do but this is as simple as I can think of (just native concurrency):
Create a static or shared volatile boolean
private static volatile boolean exceptionOccured=false
Set the above to 'true' when exception occurs:
....}catch(Exception e){
exceptionOccured=true;
}
Check this periodically in you normal thread flow:
if (exceptionOccured)
//enter you synchronized call here
the synchronized method could look something like:
public synchronized void checkAndRecover(){
//decrement a counter or other logic to identify which is the last Thread and then
//perform any recovery logic
}
I have three threads, each thread have to do some manipulation with the instance(q) of same class (Q), periodically (That's why I use Thread.sleep() in the method somecheck). Main task is to make thread execute not at the same time, so at one time can execute only one thread.
I tried to put content of run method each thread into synchronized (q){}, but I do not understand where to put notify and wait methods.
class Q {
boolean somecheck(int threadSleepTime){
//somecheck__section, if I want to stop thread - return false;
try{
Thread.sleep(threadSleepTime);
} catch (InterruptedException e) {
}
return true;
}
}
class threadFirst extends Thread {
private Q q;
threadFirst(Q q){this.q=q;}
public void run(){
do{
//Working with object of class Q
}
while(q.somecheck(10));
}
}
class threadSecond extends Thread {
private Q q;
threadSecond(Q q){this.q=q;}
public void run(){
do{
//Working with object of class Q
}
while(q.somecheck(15));
}
}
class threadThird extends Thread {
private Q q;
threadThird(Q q){this.q=q;}
public void run(){
do{
//Working with object of class Q
}
while(q.somecheck(20));
}
}
class run{
public static void main(String[] args) {
Q q = new Q();
threadFirst t1 = new threadFirst(q);
threadSecond t2 = new threadSecond(q);
threadThird t3 = new threadThird(q);
t1.start();
t2.start();
t3.start();
}
}
You don't need to put any notify() and wait() methods if you use synchronized blocks inside all of the methods, for example:
class threadFirst extends Thread {
...
public void run() {
synchronized (q) {
//your loop here
}
}
...
}