【问题标题】:Java threads not sharing static dataJava 线程不共享静态数据
【发布时间】:2012-07-16 11:25:47
【问题描述】:

我有两个线程应该共享静态变量数据(不是常量),它们必须相应地执行。但是这些线程中没有一个能够获取更新后的静态变量数据,除了每个线程都为静态变量维护自己的状态。

我已将静态变量放在单例中并将其声明为 volatile,但结果仍然相同。

有人可以提出这种方法的问题吗?可以采取什么措施来解决这个问题。

谢谢, 阿杰

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.

【问题讨论】:

  • 你能发布一段特定的代码来显示你的错误吗?
  • 愿意分享您的代码吗?我没有看到您所描述的方法存在问题
  • 能否详细说明一下,如果可能的话,举个小例子?
  • 感谢大家有兴趣解决这个问题。我已经粘贴了下面的代码

标签: java multithreading sharing static-members


【解决方案1】:

如果你打算改变它的状态,那么声明你的引用单例 volatile 不会有帮助。您需要使用由 volatile var 引用的 不可变 单例,或者使该单例中的 每个单独的 var 为 volatile。这只是在你没有原子性问题的情况下。第二种方法与根本没有单例几乎相同,只是一些 static volatile 全局变量。

【讨论】:

  • 我尝试使用静态全局 volatile 变量,但线程仍然没有得到更新的数据,这让我很困惑 :)
猜你喜欢
  • 1970-01-01
  • 2011-07-08
  • 2019-03-29
  • 1970-01-01
  • 2013-01-24
  • 2012-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多