gpt4 book ai didi

java - notifyAll() 不起作用,如何在套接字编程中通知所有线程

转载 作者:行者123 更新时间:2023-12-02 03:38:23 25 4
gpt4 key购买 nike

所有线程都可以等待,但只能通知1个线程(最后一个线程)。如何notifyAll所有线程?

public class Server {   
static Socket clientSocket;
static int count = 0 ;
static boolean listeningSocket = true;

public static void main(String[] args) throws IOException {

ServerSocket serverSocket = null;

try {
serverSocket = new ServerSocket(2343);
} catch (IOException e) {
System.err.println("Could not listen on port: 2343");
}

while(listeningSocket){
clientSocket = serverSocket.accept();

count++ ;
Serverrun myThread[] = new Serverrun[3];
myThread[count-1] = new Serverrun(clientSocket);
myThread[count-1].start();
if(count>=3){
listeningSocket = false;
}
}
serverSocket.close();
}

}

public class Serverrun extends Thread{

Socket clientSocket;

public Serverrun(Socket clientSocket) {

this.clientSocket = clientSocket;
}

public void run(){

System.out.println("abc");
String clientSentence;
String cap_Sentence;
String rd1,rd2;

try {
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
DataOutputStream outToClient2 = new DataOutputStream(clientSocket.getOutputStream());
outToClient2.writeBytes("User Login:"+'\n');
clientSentence = inFromClient.readLine();
System.out.println(" " +clientSentence+ " : login");

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("error");
}

Server s = new Server();
receivebj(s.listeningSocket);

Game g = new Game();
rd1=g.randomNum();
rd2=g.randomNum();
DataOutputStream outToClient;
try {
outToClient = new DataOutputStream(clientSocket.getOutputStream());
outToClient.writeBytes(rd1+" "+rd2+'\n');
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

synchronized void receivebj(boolean listeningSocket){

if(listeningSocket!=false){
try {

wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("work receivebj");
notifyAll();

}

}

我有 3 个客户结果 client1 和 client2 未终止:

用户登录:ant

用户登录:bird

但是 client3 终止了:

用户登录:cat

卡片数量:9 Q

<小时/>

附加类(class)游戏

public class Game { 

String randomNum() {
// TODO Auto-generated method stub

String rd1,rd2;
String[] names;
names = new String[12];
names[0] = "A";
names[1] = "2";
names[2] = "3";
names[3] = "4";
names[4] = "5";
names[5] = "6";
names[6] = "7";
names[7] = "8";
names[8] = "9";
names[9] = "J";
names[10] = "Q";
names[11] = "K";
int num = (int) (Math.random()*12);
//System.out.println("Number:"+names[num]);
return names[num];
}
}

类客户端

class Client {
public static void main(String argv[]) throws Exception {
String sentence;
String modifiedSentence1,modifiedSentence2;


BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("localhost",2343);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
modifiedSentence1 = inFromServer.readLine();
System.out.println(modifiedSentence1);
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');

modifiedSentence2 = inFromServer.readLine();
System.out.println("Number of card:"+ modifiedSentence2);
clientSocket.close();

}
}

结果服务器:

abc
ant : login
abc
bird : login
abc
cat : login
work receivebj method

最佳答案

你的多线程被严重破坏了。看来您误解了 wait()notifyAll() 的概念。这些不是在不同对象之间发送消息的消息系统。

Serverrun 中,您有 receivebj 执行 wait()同时自身同步。但是,您没有代码可以通过该对象上的 notifyAll() 来实际唤醒它。

因此,对于每个 Serverrun,您只需挂起其线程,而无需唤醒它。这是行不通的。您需要有一个通用对象Serverrun 实例可以在该对象上执行wait()

我建议回到基础知识并更多地阅读多线程和并发性,特别是 synchronization ,因为你的错误是你滥用了它。

关于java - notifyAll() 不起作用,如何在套接字编程中通知所有线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37155635/

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