2012-07-16 2 views
0

고정 변수가 아닌 정적 변수 데이터를 공유하는 두 개의 스레드가 있고 그에 따라 실행해야합니다. 그러나이 스레드 중 어느 것도 정적 변수에 대한 자체 상태를 유지하는 것 외에도 업데이트 된 정적 변수 데이터를 가져올 수 없습니다.정적 데이터를 공유하지 않는 Java 스레드

나는 하나의 변수에 정적 변수를 넣고 휘발성 변수로 선언했지만 여전히 결과는 동일합니다.

누군가가이 방법으로 문제를 제안 할 수 있으며이를 해결하기 위해 무엇을 할 수 있습니까? 당신이 그 상태를 돌연변이하려는 경우

들으, 싱글 휘발성에 참조를 선언 에이

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

+0

코드를 공유하는 데주의해야합니까? 내가 설명한 접근 방식에 문제가 보이지 않는다. – Miquel

+0

가능한 한 작은 예제로 좀 더 자세히 설명해 주시겠습니까? – Thomas

답변

3

도움이 될 수 없습니다. 휘발성 var에 의해 참조 된 불변 싱글 톤을 사용하거나 각 개별 var을 해당 싱글 톤 휘발성으로 지정해야합니다. 그리고 그것은 원 자성 문제가없는 경우에만 해당됩니다. 두 번째 접근법은 싱글 톤을 전혀 갖지 않는 것과 거의 같습니다. 단지 static volatile 개의 글로벌 바입니다.

+0

아래의 코드를 붙여 넣었습니다. 정적 전역 가변 변수로 시도했지만 여전히 스레드가 업데이트 된 데이터를 가져 오지 않습니다. –

관련 문제