【问题标题】:set and get method not working as planned设置和获取方法未按计划工作
【发布时间】:2012-12-07 01:05:34
【问题描述】:

我只是想向另一个对象发送消息,但除非我错过了一个重要因素,否则它不会完全改变。我有一个类来设置数字和另一个获取的类,获取的类取决于设置的正确数字,但是尽管 set 方法在命令行返回 0 一个,但另一个对象在使用 get 方法时返回 1。这是包含两个方法的关闭类,每个方法都从 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();
    }
}





}

【问题讨论】:

  • 我会发布与 AlexWien 和 FloatingCoder 相同的答案。尝试声明 static int close = 1;
  • 感谢您添加更多代码,这很有帮助。但是,我看不到 setClose 在哪里被调用?另外,在您的文字中,您说“set 方法返回 0”但 setClose 不返回任何内容? (返回类型为void)你的意思是println语句的输出是0吗?

标签: java multithreading get set


【解决方案1】:

可能他们都没有使用同一个 Close 对象。

Close close = new Close();

然后确保两者使用相同的关闭对象!

如果不可能,可以将字段关闭设为静态:

  public class Close {
static int close;

【讨论】:

    【解决方案2】:

    您可以只使用 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;
    
    
      }
    

    【讨论】:

      【解决方案3】:

      试试……

      static int close = 1;
      

      这样,对于 Close 类的所有实例,只有一个 int close 副本。在您使用此类的地方添加代码会有所帮助,我猜您正在为每个操作实例化新副本,这会导致您的问题。

      【讨论】:

      • 使整数静态无济于事,我已经尝试过,并且对两者都使用相同的对象,但我仍然遇到问题,它似乎没有得到正确的整数。跨度>
      【解决方案4】:

      我真的不明白你的问题。您能否介绍一下您是如何使用这个类的?

      但是,您很可能没有从所谓的 A 类和 B 类访问同一个实例。

      这就像 A 在一张纸上写东西,而 B 正在阅读另一张纸。当然 A 写的东西 B 不可读。

      【讨论】:

        猜你喜欢
        • 2014-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多