sharing a variable between different thread with synchronized methods

Hi all, i've a question about how to manage the access to variables shared between threads. Is this method correct or still there is the risk that a thread try to set the variable at the same time another is trying to get it? Thank you

public class myClass implements Runnable {
  boolean myBoolean;

 @ Override 
  public void run() {
  }

  public synchronized void setMyBoolean(boolean  input) {
    myBoolean = input;
  }
  public synchronized boolean getMyBoolean() {
    return myBoolean;
  }
}

Answers

  • edited March 2017

    If it's just a simple direct assignment, w/o expressions, volatile is pretty safe enough: O:-)

    // https://forum.Processing.org/two/discussion/21087/
    // sharing-a-variable-between-different-thread-with-synchronized-methods
    
    // 2017-Mar-01
    
    final MyClass concurrent = new MyClass();
    
    void setup() {
      new Thread(concurrent).start();
      println(concurrent.getMyBoolean());
      delay(20);
      println(concurrent.getMyBoolean());
      exit();
    }
    
    public class MyClass implements Runnable {
      protected volatile boolean myBoolean;
    
      @Override public void run() {
        setMyBoolean(true);
      }
    
      public void setMyBoolean(final boolean bool) {
        myBoolean = bool;
      }
    
      public boolean getMyBoolean() {
        return myBoolean;
      }
    }
    
  • Yes actually i used a pattern described in the second link, even if then the the atomic variables were described. I'm more confuse now, ahah. So, sorry if are a lot of question:

    What i did is wrong, or just more than needed in this case?

    What i did, just take care than two threads can't exectute at the same time the same method, or also take care to avoid interferences on the variable the method is changing? (since in the end more than one method refer to the same variable)

    If i use volatile i can directly access the variable without having methods at all, what you mean for expressions?

    Atomic variable are just for certain predefined kinds, right? I can't decide an object i want to pass or redefine is athomic, so how to protect it?

    Sorry about the questions but is a bit a complex argument for me and i think there is a lot of mess online.

    Thanks!

  • edited March 2017

    What I did is wrong, or just more than needed in this case?

    More than needed in this case. I-)

    If I use volatile I can directly access the variable without having methods at all, ...

    No need for methods if that's your taste, like mine. :P
    However, volatile is the most fragile (although faster) of all concurrency management. :-S

    What you mean for expressions?

    3 + 8, a * .5, sin(x) + cos(y), etc;

    In those cases, you're gonna need full synchronized if you intend to store the result in a concurrency field! :-<

    Atomic variable are just for certain predefined kinds, right?

    The atomic package is an advanced middle-ground between fastest volatile & slowest synchronized.

    It allows us to do some simple arithmetic operations, comparison checks, and assignments in an atomic way w/o synchronized. \m/

    In this ancient example, I've used compareAndSet() in order to guarantee that 1 Thread only could continue executing the rest of the method: :)>-

    1. https://forum.Processing.org/two/discussion/6594/best-practice-of-structuring-a-code#Item_5
    2. https://docs.Oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicBoolean.html#compareAndSet-boolean-boolean-
Sign In or Register to comment.