0
votes

Can wait() and notify() exist together in same synchronized method in JAVA and one thread enter the synchronized method and wait until other method enter the same synchronized method and notifyall ? i need to print thread name sequentially.using wait and notify and I am trying to use below code. Below is output response :

Waiting thread 5 Thread main exit Waiting thread 2 Waiting thread 3 Waiting thread 4 thread started 1 Waiting thread 1

public class Printthread implements Runnable{

Thread t ;
static int number = 1;
boolean check = true;
public Printthread(String name)
{
    t = new Thread(this,name);
    t.start();
}

@Override
public void run() {
    // TODO Auto-generated method stub
   print();     
   System.out.println(" thread EXIT " + t.getName());
}

synchronized public void print()
{
    while(Integer.parseInt(t.getName()) != number)
    {
        try {
            System.out.println(" Waiting thread  " + t.getName());
                wait();
            System.out.println(" resumed thread started " + t.getName());
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    if(Integer.parseInt(t.getName()) == number)
    {
        System.out.println(" thread started " + t.getName());
        number = number + 1;
         notifyAll();
    }

}

}

public class ThreadMain {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub\

    Printthread p5 = new Printthread("5");
    Printthread p2 = new Printthread("2");
    Printthread p4 = new Printthread("4");
    Printthread p3 = new Printthread("3");
    Printthread p1 = new Printthread("1");
    System.out.println("Thread main exit");

}

}

1

1 Answers

-1
votes

Your logic is wrong. Sync blocks aren't on the same object, since your are creating 5 different Printthread objects. For your question, yes they can exist in the same sync block. Here is the correct version :

public class PrintThread implements Runnable {
    static int number = 1;
    public PrintThread() {
        Thread t1 = new Thread(this, "1");
        Thread t2 = new Thread(this, "2");
        Thread t3 = new Thread(this, "3");
        Thread t4 = new Thread(this, "4");
        Thread t5 = new Thread(this, "5");
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        print();
        System.out.println(" thread EXIT " + Thread.currentThread().getName());
    }

    synchronized static int getNumber() {
        return number;
    }

    synchronized static void incNumber() {
        number++;
    }

    synchronized public void print() {
        while (Integer.parseInt(Thread.currentThread().getName()) != number) {
            try {
                System.out.println(" Waiting thread  " + Thread.currentThread().getName());
                wait();
                System.out.println(" resumed thread started " + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (Integer.parseInt(Thread.currentThread().getName()) == number) {
            System.out.println(" thread started " + Thread.currentThread().getName());
            incNumber();
            notifyAll();
        }
    }

    public static void main(String[] args) {
        new PrintThread();
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Thread main exit");
    }
}