gpt4 book ai didi

java - 即使在其他线程中启动,帧也会卡住

转载 作者:行者123 更新时间:2023-12-01 14:10:55 24 4
gpt4 key购买 nike

我希望在我的程序与其他播放器连接时显示一个框架。但即使框架在自己的线程中运行,当 Serversocket 尝试连接时它仍然卡住。如何防止其卡住?

public static boolean connectH(String check, String data, int k){
ServerSocket serverSocket = null;
Socket clientSocket = null;
PrintWriter out = null;
BufferedReader in = null;
message = "waiting for someone to connect to your " + check + "-game";
th = new Thread(){
@Override
public void run() {
c = new Canceler(message);
}
};
th.start();
try {
serverSocket = new ServerSocket(Integer.parseInt(data));
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Could not listen on port: " + data);
Main.m.finishgame(-1);
}


try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}

try {
out = new PrintWriter(clientSocket.getOutputStream(), true);
} catch (IOException e) {
e.printStackTrace();
}
try {
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
try {
if (in.readLine().equals(check)){
out.println(k);
c.remove();
return true;
}else{
out.println(String.valueOf(-1));
c.remove();
return false;
}
} catch (IOException e) {
e.printStackTrace();
}
c.remove();
return false;
}

最佳答案

也许这个示例 Applet 可以帮助您入门。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class Example extends JApplet
{
private JButton _jButtonStartGameServer;
private boolean _isStarted = false;
private GameServer _gameServer = null;
private JTextArea _jTextArea;
private Thread _gameServerThread = null;
private boolean _initialized = false;

public Example()
{
System.out.println("ctor...");
}

private void initComponents()
{
if (_initialized)
{
return;
}

_jButtonStartGameServer = new JButton("Start Game Server");
_jButtonStartGameServer.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (!_isStarted)
{
startupGameServer();
}
else
{
shutdownGameServer();
}
}
});
setLayout(new BorderLayout(0, 0));

_jTextArea = new JTextArea();
add(_jTextArea);
add(_jButtonStartGameServer, BorderLayout.NORTH);

_initialized = true;
}

public void init()
{
System.out.println("init... " + Thread.currentThread());
try
{
SwingUtilities.invokeAndWait(new Runnable()
{
public void run()
{
initComponents();
}
});
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
catch (InterruptedException e)
{
e.printStackTrace();
Thread.currentThread().interrupt();
}
}

public void start()
{
System.out.println("start... " + Thread.currentThread());
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
setSize(640/2, 480/2);
}
});
}

public void stop()
{
System.out.println("stop... " + Thread.currentThread());
shutdownGameServer();
}

public void destroy()
{
System.out.println("destroy " + Thread.currentThread());
shutdownGameServer();
}

void displayException(Throwable t)
{
log("Got exception: " + t);
}


void processMessageFromOtherPlayer(String msg)
{
log("Received message from other player: " + msg);
}

void gameServerThreadExiting()
{
shutdownGameServer();
}

public void startupGameServer()
{
_gameServer = new GameServer();
_gameServerThread = new Thread(_gameServer);
_gameServerThread.start();
_isStarted = true;
_jButtonStartGameServer.setText("Stop Game Server");
}

public void shutdownGameServer()
{
GameServer gs = _gameServer;
if (gs != null)
{
gs.cancel();
}
Thread t = _gameServerThread;
try
{
if (t != null)
{
t.join();
}
}
catch (InterruptedException ie)
{
Thread.currentThread().interrupt();
}
_gameServer = null;
_jButtonStartGameServer.setText("Start Game Server");
_isStarted = false;
}

void log(final String msg)
{
System.out.println("Log (" + Thread.currentThread() + "): " + msg);
Runnable r = new Runnable()
{
public void run()
{
StringBuilder sb = new StringBuilder(_jTextArea.getText());
sb.append(msg);
sb.append("\n");
_jTextArea.setText(sb.toString());
}
};
if (SwingUtilities.isEventDispatchThread())
{
r.run();
}
else
{
SwingUtilities.invokeLater(r);
}
}

class GameServer implements Runnable
{
volatile ServerSocket _ss = null;
volatile Socket _s = null;
volatile InputStream _sis = null;
volatile boolean _cancel = false;

public void run()
{
try
{
_ss = new ServerSocket(9999);
log("Waiting for other player, listening on port: " + _ss.getLocalPort());
_s = _ss.accept();
log("Connection accepted from: " + _s.getRemoteSocketAddress());
_sis = _s.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(_sis, "UTF-8"));
while (true)
{
if (_cancel)
{
break;
}
final String msg = br.readLine();
if (msg == null)
{
// connection closed / lost
break;
}
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
processMessageFromOtherPlayer(msg);
}
});
}
}
catch (final Throwable t)
{
// don't show the user the exception if the user is canceling
if (!_cancel)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
displayException(t);
}
});
}
else
{
// even if the user is canceling, we might be interested in the exception, though
t.printStackTrace();
}
}
log("Game server thread exiting...");
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
gameServerThreadExiting();
}

});
}

public void cancel()
{
_cancel = true;

ServerSocket ss = _ss;
if (ss != null)
{
try
{
ss.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}

Socket s = _s;
if (s != null)
{
try
{
s.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}

InputStream sis = _sis;
if (sis != null)
{
try
{
sis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
}

关于java - 即使在其他线程中启动,帧也会卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18538248/

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