我只是想向另一个对象发送消息,但除非我错过了一个重要因素,否则它不会完全改变。我有一个类来设置数字和另一个获取的类,获取的类取决于设置的正确数字,但是尽管 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();
}
}
}