gpt4 book ai didi

java - 等待直到条件变为真

转载 作者:行者123 更新时间:2023-12-01 10:57:54 25 4
gpt4 key购买 nike

我得到了三个类,分别是 TCPClient.java、TCPProtocol.java、TCPServer.java 。

*更新,我也粘贴了完整的 TCPServer.java

首先,用户将在 TCPProtocol GUI 窗口中输入文本(通过使用 JTextarea),然后单击“发送”按钮。如果用户点击“发送”,TCPClient将从TCPProtocol获取输入,最后发送到TCPServer。所以问题是,我的 TCPClient 从未从 TCPProtocol 获取输入。

TCPServer.java

import java.net.*;
import java.io.*;

public class TCPServer {

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

//TCP component
ServerSocket serverSocket = null;
Socket clientSocket = null;
int port = 8081;
TCPProtocol nis = new TCPProtocol();//create an object to call method in protocol class

try {
serverSocket = new ServerSocket(port);
System.out.println("Waiting for connection...");
System.out.println();
} catch (IOException e) {
System.out.println(e.toString() + " :Could not listen on port: " + port);
System.exit(1);
}

try {
clientSocket = serverSocket.accept();
System.out.println("TCP Session established.");
nis.frame.setVisible(true);
nis.frame.setAlwaysOnTop(true);
System.out.println();

} catch (IOException e) {
System.out.println(e.toString() + " :Accept failed.");
System.exit(1);
}

PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

String inputLine, outputLine;
outputLine = nis.processAnswer(null);//get Learn Learn English line
out.println(outputLine);//and send the line out to client
nis.Jservertxt.setText(outputLine);

while ((inputLine = in.readLine()) != null) {
outputLine = nis.processAnswer(inputLine);
out.println(outputLine);
nis.Jservertxt.setText(outputLine);
if (outputLine.equalsIgnoreCase("Don't miss me! Bye Bye!"))
break;
else if (outputLine.equalsIgnoreCase("Thank For Your Guessing. Have a Nice Day =)"))
break;
}

//End connection
out.flush();
out.close();
in.close();
nis.frame.setVisible(false);
if (!clientSocket.isClosed())
clientSocket.close();
nis.frame.setVisible(false);
if (!serverSocket.isClosed())
serverSocket.close();
nis.frame.setVisible(false);

System.out.println("TCP session closed.");
}
}

这是我的 TCPProtocol.java 代码

public volatile boolean getPressed;
Jsend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

String Data = Jusertxt.getText();
Jusertxt.setText(null);
System.out.println(Data);
getPressed=true;
}
});
}

public boolean getPressed()
{
return getPressed;
}

我没有在此处粘贴完整的代码,因为我只是显示出现问题的部分。

这是我的 TCPClient 代码

import java.io.*;
import java.net.*;

public class TCPClient{

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

//TCP component
int port = 8081;
String host = "localhost";
Socket nisSocket = null;
PrintWriter out = null;
BufferedReader in = null;
TCPProtocol pro = new TCPProtocol();

try {
nisSocket = new Socket(host, port);
out = new PrintWriter(nisSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(nisSocket.getInputStream()));
} catch (UnknownHostException e) {
System.out.println(e.toString() + " :Don't know about " + host);
System.exit(1);
} catch (IOException e) {
System.out.println(e.toString() + " :Couldn't get I/O for the connection to " + host);
System.exit(1);
}

BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer, fromUser;

while ((fromServer = in.readLine()) != null) {
System.out.println("Server: " + fromServer);
if (fromServer.equalsIgnoreCase("Don't miss me! Bye Bye!"))
break;
if (fromServer.equalsIgnoreCase("Thank For Your Guessing. Have a Nice Day =)"))
break;

if (pro.getPressed())
{
fromUser = pro.Jusertxt.getText();
if (fromUser != null) {
System.out.println("\nClient: " + fromUser);
out.println(fromUser);
}
}
System.out.println("fail liao");

}
System.out.println("try again");
//End connection
out.flush();
out.close();
in.close();
stdIn.close();

//if (!nisSocket.isClosed())
nisSocket.close();
System.out.println("TCP session closed.");
}
}

那么,如果只有 getPressed() 方法为 true,我如何才能让 TCPClient.java 进行处理。

我尝试使用 volatile boolean 变量,但效果不佳。或者我应该使用同步?

<小时/>

更改为从服务器到客户端调用GUI代码后解决

while ((fromServer = in.readLine()) != null) {
System.out.println("Server: " + fromServer);
if (fromServer.equalsIgnoreCase("Don't miss me! Bye Bye!"))
break;
if (fromServer.equalsIgnoreCase("Thank For Your Guessing. Have a Nice Day =)"))
break;

pro.frame.setVisible(true);
pro.frame.setAlwaysOnTop(true);
pro.Jservertxt.setText(fromServer);
if(pro.getPressed()){
fromUser = pro.getMessage();
if (fromUser != null) {
System.out.println("\nClient: " + fromUser);
out.println(fromUser);
}
}
}

最佳答案

这是一个主线程(将其视为您的 TCPClient)的小示例,它等待用户单击按钮(将这部分视为您的 TCPProtocol):

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class Test extends JFrame {

public Object lock = new Object();
private JTextArea area = new JTextArea();

public Test() {
super("test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton send = new JButton("send");
send.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
synchronized (lock) {
lock.notify();
}

}
});
add(area);
add(send, BorderLayout.SOUTH);
}

public String pullText() {
String result = area.getText();
area.setText("");
return result;

}

public static void main(String[] args) {
Test t = new Test();
t.setSize(200, 200);
t.setVisible(true);

while (true) {
synchronized (t.lock) {
try {
t.lock.wait();
System.out.println(t.pullText());
}
catch (InterruptedException e) {
e.printStackTrace();
}

}
}

}

}

也许您可以对您的系统应用类似的方法。

警告:这只是一个快速而肮脏的示例,旨在为您提供想法,请仔细检查。一般来说,处理同步时应该非常小心。

编辑:作为起点,您可以像这样修改 TCPProtocol 类(您发布的部分):

public volatile boolean getPressed;

private Object lock = new Object(); // added lock for wait/notify coordination
private String Data; //changed scope to make it accessible in getMessage()

Jsend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

synchronized(lock){
Data = Jusertxt.getText(); // no more a local variable, it's an instance attribute now, see declaration above
Jusertxt.setText(null);
System.out.println(Data);
getPressed=true;
lock.notify(); // notify client that user performed action
}
});
}

public boolean getPressed(){
synchronized (lock) {
try {
lock.wait(); // wait for user to perform action...
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
return getPressed; // ...then return value (I guess getPressed will always be true at this point)
}

public String getMessage(){ // call this in TCPClient instead of pro.Jusertxt.getText()
return Data;
}

我添加了一个锁对象来启用等待/通知机制,并将其应用于您的 actionPerformed 和 getPressed 方法。我还更改了 Data 变量的范围,并添加了一个公共(public)方法来获取其值 (getMessage())。如果你仔细检查你的代码,你会发现你总是从 TCPClient.java 中的 pro.Jusertxt.getText() 得到 null,因为你在 actionPerformed 中将其设置为 null。因此将 TCPClient.java 中的 pro.Jusertxt.getText() 更改为 pro.getMessage()。

// [...]
fromUser = pro.getMessage(); // used to be fromUser = pro.Jusertxt.getText();
// [...]

如果您让这个原始版本正常工作,那么您可以改进和重构它,使其更加干净和一致。 (例如,我怀疑您实际上并不需要 getPressed 变量,因为它实际上总是 true)

我承认我没有测试此代码,因此您可能需要稍微修复或调整它,但我希望它可以澄清我的意思,您可以将我发布的代码应用到您的系统。

希望有帮助。

祝你好运:-)

关于java - 等待直到条件变为真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33523294/

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