0

these are just sample codes to ask my question the other statements are omitted here the instance of NewClass is being passes both to Foot and Hand objects and hence all the instances NewClass,foot and hand share the variable sno of NewClass.

public class NewClass  {
     volatile int  sno = 100;

    public static void main(String args[])
    {
      NewClass n = new NewClass();  

      Foot f = new Foot(n); 
      Hand h = new Hand(n);

      f.start();
      h.start();

    }                
}

public class Foot implements Runnable{

    Thread t;
    NewClass n;
    public Foot(NewClass n)
    {
        this.n = n;
    }
    public void run(){
        for(int i=0;i<100;i++)
        {
            System.out.println("foot thread "+ i+" "+n.sno);
            n.sno=(-1)*n.sno;

             Thread.sleep(1); // surrounded by try-catch
        }
    }   
}

public class Hand implements Runnable {
    Thread t;
    NewClass n;
    public Hand(NewClass n)
    {
        this.n = n;
    }
    public void run(){
        for(int i=0;i<100;i++)
        {
            System.out.println("hand thread "+ i+" "+n.sno);
            n.sno=(-1)*n.sno;  
                Thread.sleep(1);  // surrounded by try -catch
        }
    }   
}

here the sign of seq.no is changing everytime but when used by the other thread the change is many times not reflected as if the updation is taking time.so please help ,

honey
  • 3
  • 2
  • In your code you do not use the Thread in your Runnable. Normally one does not place the Thread that is going to run the Runnable inside the Runnable. See http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread – gfelisberto May 12 '16 at 08:42

1 Answers1

0

It is not taking to long to update.

System.out.println("foot thread " + i + " " + n.sno);
n.sno=(-1)*n.sno;

When you have this happening in two threads running in parallel they may be watching the value as positive at the same time. So they both change the value to negative. If you want to control the change you need a semaphore.

In NewClass:

volatile int sno = 100;
final Object semaphore = new Object();

And in your two Runnables:

synchronized (n.semaphore) {
    System.out.println("foot thread " + i + " " + n.sno);
    n.sno = (-1) * n.sno;
}
gfelisberto
  • 1,655
  • 11
  • 18
  • thank you,the problem is solved can u just explain me a bit more – honey May 12 '16 at 09:20
  • Please don't forget to vote and accept the answer. What part of the answer you need me to explain in more detail? – gfelisberto May 12 '16 at 09:34
  • ya ok,how does semaphore solved the updation problem and which part should be in the synchronization block,the part to be updated or the part to be read. – honey May 12 '16 at 13:33
  • Hello, join the chat http://chat.stackoverflow.com/rooms/info/111782/talk-with-6324297 and I'll explain – gfelisberto May 12 '16 at 15:00
  • i cannot join the chat i am new to stackoverflow..so if u can give me explanation of above ,it will be very helpful as i need to apply it some where else. – honey May 12 '16 at 17:13
  • when you have `synchronized (some_object) { ... }` it means that the JVM will only allow a single thread to execute the block. These code blocks should be as small as possible as they are preventing other threads from executing. In your case only the `n.sno = (-1) * n.sno;` actually needs to be inside the synchronized for the code to be _correct_ . But if you want the printed messages to also be correct leave them inside the block. For more information i recommend the [Oracle Java Tutorials](https://docs.oracle.com/javase/tutorial/essential/concurrency/index.html) – gfelisberto May 13 '16 at 08:47