java为什么我的count++在Thread类中的run()方法中重置?
每次从客户机到服务器建立连接时,客户机都会向服务器发送一条字符串消息“11”,当服务器收到字符串消息“11”时,它会运行count++。然后进行了两次连接,这将使count++中的count=2运行两次,但是当客户端连接时,我检查了它,客户端将字符串消息“11”正确地发送到服务器,但是计数保持为1,并且从不进入if(count==2)块。已经测试和寻找了几个小时,但似乎无法找到问题所在。请帮忙!多谢各位
客户端的代码片段:
Socket s = new Socket(hostname, port); // Plug in the socket to connect to the server
pw = new PrintWriter(s.getOutputStream()); //Instance of sending it out
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
public void run() { //Deal with reading from server and print them out
try {
pw.println("11"); //Sends the message when connection is made to the server
pw.flush();
} catch (Exception e) {
e.printStackTrace();
}
try{
while(true){
String line = br.readLine(); //Read in the message from server
if(line.equals("12")){ //When finally receives the string message "12" from server
button.setBackground(Color.white);
button.addActionListener(sl);
}
int update = Integer.parseInt(line);
if(update < 10){
current-= update;
}
}
} catch (IOException ioe){
System.out.println("ioe in ChatClient.run: " + ioe.getMessage());
}
}
服务器线程的代码段代码:
PrintWriter pw = new PrintWriter(s.getOutputStream());
public void run(){
try{
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
while(true){
String line = br.readLine(); //blocking //Keep reading in messages
if(line.equals("11")){ //Tested it out and does receive "11" whenever a client connects and prints out "11"
count++; //But the problem is count stays as 1 every time it connects and never becomes 2
System.out.println(line);
System.out.println(count);
}
if(count == 2){ //Never able to reach inside this block of code
pw.println("12");
pw.flush();
count++;
}
}
} catch(IOException ioe){
System.out.println("ioe in ChatTHread: " + ioe.getMessage());
}
}
编辑-服务器代码:
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.Vector;
public class Server {
private int count = 0;
private Vector<FFThread> ctVector = new Vector<FFThread>(); //Parametrized
public Server(int port){
try{
ServerSocket ss = new ServerSocket(port); //Server socket to connect to the port
while(true){
Socket s = ss.accept(); // Listens for a connection to be made to this "s" socket and accepts it.
FFThread ct = new FFThread(s, this); //Get new socket access thread
ctVector.add(ct); //Appends the specified element "ct" to the end of this Vector.
ct.start();
}
} catch(IOException ioe){
System.out.println("ioe in ChatServer: " + ioe.getMessage());
}
}
public int counter(){
this.count = 0;
count++;
return count;
}
public void sendMessage(String message, FFThread ct){
for(FFThread c : ctVector ){
if(!c.equals(ct)){ //Two different clients
c.sendMessage(message);
}
}
}
public void removeThread(FFThread ct){
ctVector.remove(ct);
}
public static void main(String [] args){
Scanner scan = new Scanner(System.in);
System.out.print("What port? ");
int port = scan.nextInt();
new Server(port);
}
}
编辑-服务器的线程类:
import java.awt.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
public class FFThread extends Thread {
private Socket s;
private Server cs;
private PrintWriter pw;
private int count = 0;
boolean ready = false;
public FFThread(Socket s, Server cs){
this.s = s;
this.cs = cs;
try{
this.pw = new PrintWriter(s.getOutputStream()); //Set up to send messages to clients
} catch(IOException ioe){
System.out.println("ioe in ChatThread constructor: " + ioe.getMessage());
}
}
# 1 楼答案
您的服务器类为每个传入连接启动一个新线程(实例
FFThread
)。您的FFThread
的run
方法不执行count++
,但它必须在局部变量上执行,因为它没有访问服务器类中的count
变量。因此,每个线程将其自身的count
从0
增加到1
,并且它永远不会达到两个您的
run
方法应该增加(并测试)服务器实例的count
变量,以使该计数达到2。您必须以线程安全的方式(即使用同步方法)增加它我相信将以下内容添加到服务器类中会起作用:
然后,在
FFThread
类中,使用this.cs.getCount()
和this.cs.incCount()
读取并递增计数