Swing是一个单线程框架,这意味着在事件调度线程的上下文中执行的任何阻塞或长时间运行的操作都将阻止它处理事件队列,从而使应用程序挂起。
它也不是线程安全的,因此您永远不要尝试从EDT外部修改任何UI组件的状态。
查看Swing和Worker线程中的并发性以及SwingWorker中的更多细节
public class ServerSocketWorker extends SwingWorker<Void, String> { private Jtextarea ta; public ServerSocketWorker(Jtextarea ta) { this.ta = ta; } @Override protected void process(List<String> chunks) { for (String text : chunks) { ta.append(text); } } @Override protected Void doInBackground() throws Exception { ss = new ServerSocket(1234, 3); while (true) { publish("nEsperando conexiones..."); Socket s = ss.accept(); publish("nConexión entrante: " + s.getRemoteSocketAddress()); conexiones++; //System.out.println("Debug: conexiones SERVER: " + conexiones); MultiThread mt = new MultiThread(s, conexiones); mt.start(); /////////////////////////////////////////////////////////////// } } @Override protected void done() { stopServer(); //?? }}要启动它,您可以使用类似…
public void iniciarServer() { ServerSocketWorker worker = new ServerSocketWorker(textareaToAppendTo); worker.execute();}举个例子



