2012-07-16 106 views
0

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

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

有人可以提出这种方法的问题,以及可以做些什么来解决这个问题。

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. 
+3

你能发布一段代码来显示你的错误吗? – MoRe 2012-07-16 11:28:48

+0

小心分享您的代码?我没有看到您所描述的方法存在问题 – Miquel 2012-07-16 11:28:55

+0

您是否可以详细说明一下,如果可能的话用一些小例子? – Thomas 2012-07-16 11:29:43

回答

3

声明你的参考单挥发性是不会帮助,如果你打算发生变异的状态。你需要用挥发性变种要么使用简称的不变单,或使在单挥发性每一个人变种。只有在你没有原子性问题的情况下。第二种方法几乎与没有单身人士相同,只是一些static volatile全局变量。

+0

我试图用静态全局volatile变量,但仍然线程不会得到更新这令我感到困惑了大量的数据:) – 2012-07-17 05:58:51