0

I have two threads running t1 and t2. When t2 notifies t1, immediately t2 should go to wait state. However, this is not possible as once it notifies t1, it should finish its current process and only after the end of the current thread execution, the t1 execution starts. But i would like to start t1 immediately after t2 notifies so that i can put t2 in wait state for t1 to notify inside the while loop. Is this possible with synchronized block?. I have tried the following code that does not work. I have also commented the coding lines to mention the way i would like to code.

    public void passNo(int data)//thread t1
        {
            this.data1=data;
            synchronized(thread2)
            {
           System.out.println("thread1 running");
            data1=data1+100;
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
             System.out.println("thread1 going to notify thread two");
            thread2.notify();
           /* try {
                thread1.wait();
            } catch (Exception e) {
                e.printStackTrace();
            }*/
            }//sync
            try {
                Thread.sleep(1000);
            } catch (Exception e) {}

               System.out.println("im done, receiver go");
                //}
            }
    public void ramos(int data)//thread t2
        {
            synchronized(thread1)
                {
            try{
            System.out.println("I am thread 2 waiting for thread 1");   
            thread1.wait();//Problem-not notified ever by sender
            System.out.println("Notified by sender thread");}
            catch(InterruptedException ex){}
            System.out.println("I am released");
             n=obj.getInteger();
             setInteger();
             System.out.println("Notified");
            }//sync
            j++;
            //}//while
        }

class ClaObj
{
    public static void main(String[] args) 
    {
        Sender s=new Sender();
        Receiver r=new Receiver();
        r.classobj(s);
        Thread thread1 = new Thread(s);
        Thread thread2 = new Thread(r);
        s.sendthrobj(thread1); 
        r.recvthobj(thread1);
        thread1.start();
        thread2.start();

    }
}
Struse
  • 75
  • 3
  • 13
  • here which object is shared between thread1 and thread2. I think you are wrongly doing synchronisation of Thread objects instead of synchronising the object that both threads are sharing – Tom Sebastian Nov 18 '15 at 04:58
  • Thread1 is thread2's object and vice versa. – Struse Nov 18 '15 at 05:03
  • Consider your method `passNo` where you are synchronising `thread2` object, but you are not accessing any resource/ attributes /methods of `thread2`. Then why are you synchronising it? Locking is to be done on shared object not on thread objects – Tom Sebastian Nov 18 '15 at 05:09
  • Ca you provide the `Thread`s creation code also. – Tom Sebastian Nov 18 '15 at 05:17
  • yes. I have passed the instances of thread1 to both the threads from the main class. Made the editing – Struse Nov 18 '15 at 05:20
  • 2
    Look into some of the classes in java.util.concurrent. They're a lot easier to think about than synchronized, wait, and notify. – David Ehrmann Nov 18 '15 at 05:30
  • You may need to synchronise sender or receiver object ,not the thread. Better go with @David Ehrmann comment – Tom Sebastian Nov 18 '15 at 05:57
  • Do u mean the class object? – Struse Nov 18 '15 at 06:10
  • In which class(es) the `ramos` and `passNo` methods are defined? I thought they are in `Receiver`, `Sender` classes. Or please add those details too. – Tom Sebastian Nov 18 '15 at 06:47
  • It's not clear to me what you're trying to do, but your use of wait() and notify() looks vulnerable to _lost notifications_. http://stackoverflow.com/questions/26590542/java-lang-illegalmonitorstateexception-object-not-locked-by-thread-before-wait/26593239#26593239 – Solomon Slow Nov 18 '15 at 15:14
  • @DavidEhrmann is right. The wait()/notify() mechanism is a low-level primitive that's meant for building higher-level synchronization objects. If you're writing code that's meant to be _useful_ (as opposed to writing code in order to learn how Java works), Then your threads probably should coordinate their activities through one of the higher-level objects that the library already provides for you instead of trying to debug your own. Semaphores, barriers, queues, ... There's quite a few useful classes in `java.util.concurrent`. – Solomon Slow Nov 18 '15 at 15:20
  • @Tom Sebastian yes they are in different classes. I used class object and it worked. Thank you very much. – Struse Nov 18 '15 at 18:49
  • I am looking in to some of the concurrency classes. Thanks everyone for your valuable suggestions. – Struse Nov 18 '15 at 18:51

0 Answers0