gpt4 book ai didi

java - 从另一个线程的类访问一个线程类的公共(public)静态数组列表

转载 作者:行者123 更新时间:2023-12-02 07:21:15 24 4
gpt4 key购买 nike

我正在尝试访问

public static List<ChatThread> Chat_list of my ChatThread Class 

从我的客户端类的 run() 方法中,但我不断得到一个空数组(事实上,它在此时抛出异常:线程“Thread-2”java.lang.NullPointerException 中的异常 java.lang.NullPointerException )

并且我非常确定该 arrayList 存在并且不为空(因为我在 ChatThread 类中对 arrayList 进行了测试)。看看我的代码。请我需要你的帮助来做什么。

谢谢。

这是包含 arrayList 的类:

public class ChatThread extends Thread {


private Socket sc;
private String cherry_name;
private String passwd;
public static List<ChatThread> Chat_list = new ArrayList<ChatThread>(); //THE STATIC ARRAY LIST
private BufferedReader br;
private BufferedWriter bw;
public ChatThread(Socket sc){
try {
this.sc=sc;
System.out.println(sc);
br = new BufferedReader(new InputStreamReader(sc.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(sc.getOutputStream()));
String help = br.readLine();
this.cherry_name=help.split("@")[0];
this.passwd=help.split("@")[1];
System.out.println(this.cherry_name);
System.out.println(this.passwd);
Chat_list.add(this); //This is where i add it to the arrayList
if(Chat_list.isEmpty()) //This is where i did the test
System.out.println("I am empty");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void run(){
//Comparaison of information with that in the database
try{
bw.write("success");
bw.newLine();
bw.flush();

while(true){

}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public Socket getSc() {
return sc;
}

public String getCherry_name() {
return cherry_name;
}


}

As for the Client class :

public class Client extends Thread {
private Socket socket;
private BufferedReader br;
private BufferedWriter bw;
private ChatThread th;
private String cherry_name;
public Client(String cherry_name,String passwd){
try
{
socket = new Socket("127.0.0.1", 8888);
this.cherry_name=cherry_name;
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write(cherry_name+"@"+passwd);
bw.newLine();
bw.flush();
}
catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Erreur lors de la lecture du socket");
e.printStackTrace();
}
}
@SuppressWarnings("deprecation")
public void run()
{
try {
String help = br.readLine();
if(help.equals("failed")){
this.notify();
this.destroy();
socket.close();
}
else{
if(ChatThread.Chat_list.isEmpty()) System.out.println("Empty array!!!"); //This is where it says the array is empty whereas it wasn't the case in the ChatThread Class
for(ChatThread ct : ChatThread.Chat_list){
if(cherry_name.equals(ct.getCherry_name())){
th=ct;
break;
}
}
while(true){

}

}

} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Error whilst reading from the socket");
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("Interruption");
e.printStackTrace();
}

}

public Socket getSocket() {
return socket;
}
}

还有我的服务器类:

public class Server {

public static void main(String[] args) {
try {
ServerSocket server =new ServerSocket(8888);
Socket sc;
System.out.println("Server Started");
while(true){
sc=server.accept();
System.out.println("New Connection");
new ChatThread(sc).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

实例化 Client 类的主类:

public class help {

public static void main(String[] argv) {

new Client("Jerry","Smith").start();
}

}

最佳答案

对两个线程之间共享的可变对象的每次访问都必须以同步方式进行访问。不同步会导致像您所看到的可见性和一致性问题。

你不应该像这样暴露一个ArrayList(即使没有多个线程,公共(public)静态可变对象已经是一个非常糟糕的做法)。相反,您应该将其封装在您自己的对象中,并确保每次访问都正确同步。

在看不到任何代码行的情况下很难给出更具体的建议。

关于java - 从另一个线程的类访问一个线程类的公共(public)静态数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14183485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com