0

I have two threads which are supposed to share the static variable data(not constant) and they have to execute accordingly. But none of these threads are able to get the updated static variable data, besides each maitaining it's own state for the static variable.

I have placed the static variable in a singleton and declared it as volatile, still the result is same.

Can someone suggest the problem with this approach, and what could be done to resolve this.

Thx, Aj

below is the code

following is the singleton class

***************************************************************************
public class ThreadFinder {

    public static String pageName;//HashMap threadInfo = new HashMap(); //new HashMap();

private static ThreadFinder singletonObject;
/** A private Constructor prevents any other class from instantiating. */
private ThreadFinder () {
    }
public static synchronized ThreadFinder getSingletonObject() {
    if (singletonObject == null) {
        singletonObject = new ThreadFinder();
    }
    return singletonObject;
}
public static synchronized void setPageName(String Name) {

    pageName = Name;
    //return;
}
public static synchronized String getPageName() {

    return pageName;
    //return;
}
public Object clone() throws CloneNotSupportedException {
    throw new CloneNotSupportedException();
}
//public static void main(String args[])
//{
    //ThreadFinder.getSingletonObject().setPageName("IDEN");
    //System.out.println("page name--------->"+ThreadFinder.getSingletonObject().getPageName());
    //ThreadFinder.getSingletonObject().setPageName("THER");
    //System.out.println("page name--------->"+ThreadFinder.getSingletonObject().getPageName());
//}
}

********************************************************************************

from page 1 below code executes and sets the singleton page variable value to "A" and first pollertimer thread uses the page value as A.

m_poller.setModbusMaster(this.m_connection.getModbusMaster());
m_poller.addListener(this);
ThreadFinder.getSingletonObject().setPageName("A");  //Setting the page name here
PollerTimer polerTimerThread = new PollerTimer(period,"A");  // this is thread

*********************************************************************************

from page2 below code executes and sets the singleton page variable value to "B" and second pollertimer thread uses the page value as B.

m_poller.setModbusMaster(this.m_connection.getModbusMaster());
m_poller.addListener(this);
ThreadFinder.getSingletonObject().setPageName("B");  //Setting the page name here
PollerTimer polerTimerThread = new PollerTimer(period,"B");  // this is thread

***********************************************************************************

Now when first pollertimer thread queries page value using getPageName() it is getting value A instead of B, though it was updated to B by the second thread.
4
  • 3
    Can you post a specific piece of code showing your error? Commented Jul 16, 2012 at 11:28
  • Care to share your code? I don't see problems with your approach as described Commented Jul 16, 2012 at 11:28
  • Could you elaborate a bit, if possible with some small example? Commented Jul 16, 2012 at 11:29
  • Thx all for showing interest in resolving this. I have pasted the code below Commented Jul 17, 2012 at 5:45

1 Answer 1

3

Declaring your reference to the singleton volatile is not going to help if you intend to mutate its state. You need to either use an immutable singleton referred to by a volatile var, or make each individual var in that singleton volatile. And that's only in case you don't have atomicity issues as well. The second approach is pretty much the same as not having a singleton at all, just a number of static volatile global vars.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried with static global volatile variable, but still threads are not getting updated data which baffles me a lot :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.