[Solved] Printing a statement using threads in Java [closed]


I think your problem is in this method:

synchronized void PrintMsg(String msg) {
    System.out.println(msg);
    try {
        wait();
    } catch (InterruptedException e) {
        System.out.println("Exception");
    }
    System.out.println(msg);
    notify();
}

The thread that call it are going to call wait() which causes them to wait indefinitely for someone to call notify(). But there are no other calls to notify() so they all will stop there.

Also, because the method is synchronized each thread is also waiting on it’s own NewThread instance. I think you meant to have all threads waiting and notifying on the same object?

From comments:

want to wait a thread until it finishes writing a part of statement. It should be like this : Thread 1 prints “Humphry Dumprey” Thread 2 prints “went to the hill” Thread 3 prints “to fetch a pail of water” and these three threads should execute in sequence such that the statement gets printed in right sequence.

I never understand these sorts of questions. The whole point of threads are that the run asynchronously in parallel. If you want them to print 3 things in a row then 1 thread should be used instead.

If you need to do this for some assignment then there a couple different ways you can do it.

  • Each thread could synchronize on the same AtomicInteger. Thread #1 would do its print if the integer was 1, thread #2 when it is 2, …. You could pass in the order as a value field to the NewThread constructor. After they print they values they increment the integer and notifyAll().

    static final AtomicInteger counter = new AtomicInteger(1);
    ...
    synchronized (counter) {
        // looping like this is always recommended
        while (counter.get() != value) {
           counter.wait();
        }
        System.out.println(...);
        counter.incrementAndGet();
        counter.notifyAll();
    }
    
  • You could use 2 CountdownLatch so thread #2 calls countDown1.await(); and thread #3 waits for countDown2.await();. Then after thread #1 prints its message it calls countDown1.countDown() and after thread #2 prints its message it calls countDown2.countDown().

5

solved Printing a statement using threads in Java [closed]