2012-12-07 19 views
0

我只是試圖發送消息到另一個對象,但它不完全改變,除非我錯過了一個重要因素。我有一個類來設置數字和另一個類獲取,獲取的類取決於正確的數字被設置,但儘管set方法返回0一個命令行,另一個對象返回1時,使用get方法。這在靠近類whihc保持兩者的方法,每種方法是從A類和B類稱爲設置並得到方法不按計劃工作

public class Close { 
    static int close =1; 

    public synchronized void setClose(int x, Client) 
    { 
     /* 
     while(turn !=0){ 
      try{ 
       wait(); 

      } 
      catch(Exception e) 
      { 
       e.printStackTrace(); 
      } 
     }*/ 
     close = x; 
     System.out.println("set "+close); 
    } 

    public synchronized int getClose(ClientHandeler) 
    { 
     int x; 
     /* 
     while(turn==0){ 
      try{ 
       wait(); 
      }catch(Exception e) 
      } 
     }*/ 
     System.out.println("get "+close); 
     x = close; 
     return x; 
    } 

} 

這是在其中我希望從獲得接近整數的處理類

import java.io.*; 
import java.net.*; 
import java.util.*; 

public class ClientHandler implements Runnable { 

private BufferedReader reader; 
private Socket sock;//refering to socket class 
//listens for the socket 
private Server server; // call-back reference to transmit message to all clients 
Client c = new Client(); 

public ClientHandler(Socket clientSocket, Server serv) { 

    try { 
      sock = clientSocket; 
      server = serv; 

      InputStreamReader isReader = new InputStreamReader(sock.getInputStream()); 
      reader = new BufferedReader(isReader); 



    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
} 

public void run() { 

    String message; 




    try { 

     int x = c.c.getClose(this); 
     while(x!=0){ 
     x = c.c.getClose(this);//calls client and within client there is the close 
      System.out.println(x); 

      Thread ct= Thread.currentThread(); 
      ct.sleep(3000); 
      while ((message = reader.readLine()) != null) { 

       System.out.println("read " + message); 

       server.tellEveryone(message); 
      } 
     }  
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
} 





} 
+0

我會發布與AlexWien和FloatingCoder相同的答案。 嘗試聲明靜態int close = 1; –

+0

感謝您添加更多代碼,這很有幫助。但是,我沒有看到setClose被調用的位置?另外,在你的文本中你說「set方法返回0」,但setClose不返回任何東西? (返回類型是void)你的意思是println語句的輸出是0嗎? – FloatingCoder

回答

1

可能它們都不使用相同的Close對象。

Close close = new Close(); 

然後確保兩個使用相同的關閉對象!

如果這是不可能的,現場近可以進行靜態:

public class Close { 
static int close; 
0

你可以使用一個AtomicInteger。

public class Close { 
AtomicInteger close = new AtomicInteger(1); 

public void setClose(int x) 
{ 
    /* 
    while(turn !=0){ 
    try{ 
     wait(); 

    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    }*/ 

    close.set(x); 
    System.out.println("set "+close.get()); 
} 
public int getClose() 
{ 
    int x; 
    /* 
    while(turn==0){ 
     try{ 
      wait(); 
     }catch(Exception e) 
    }*/ 
    System.out.println("get "+close.get()); 
    x = close.get(); 
    return x; 


    } 
0

的Try ...

static int close = 1; 

這樣只會有整型關閉的一個副本,關閉類的所有實例。在你使用這個類的地方添加代碼會有所幫助,我猜你正在爲每個操作實例化新副本,這會導致你的問題。

+0

使整數靜態沒有幫助,我已經嘗試,以及使用相同的對象,我仍然有問題,它似乎並沒有得到正確的整數。 –

0

我真的不明白你的問題。你可以給你一些關於你如何使用這個課程的片段嗎?

然而,它看起來很可能你不訪問相同的情況下形成的所謂的A類和B

它就像一個被寫在一張紙的東西,而B讀取另一片的紙張。當然A寫的東西不可讀B.