Starting ServerSocket on Java with User Interface(Swing) Freezes -
good day,
i have infinite loop serversocket, working fine... problem when try start serversocket button. user interface "freeze" don't move, anything, server , fine, here have screenshot:
http://i.gyazo.com/15d331166dd3f651fc7bda4e3670be4d.png
when press button "iniciar" means start server, user interface freezes (serversocket infinite loop). i can't change code because working fine.
public static void iniciarserver() { try { appendstring("\nservidor iniciado."); system.out.println("asdasd"); } catch (badlocationexception e1) { e1.printstacktrace(); } try { ss = new serversocket(1234, 3); while (true) { system.out.println("esperando conexiones..."); appendstring("\nesperando conexiones..."); socket s = ss.accept(); system.out.println("conexión entrante: " + s.getremotesocketaddress()); appendstring("\nconexión entrante: " + s.getremotesocketaddress()); conexiones++; //system.out.println("debug: conexiones server: " + conexiones); multithread mt = new multithread(s, conexiones); mt.start(); /////////////////////////////////////////////////////////////// } } catch (ioexception e) { system.out.println("error server: " + e.getmessage()); } catch (badlocationexception e) { e.printstacktrace(); } stopserver(); }
appendstring(); add text jtextpane, doesnot work because ui freezes.
is there way user interface don't freeze infinite loop?
thanks!
swing single threaded framework, meaning blocking or long running operation executed within context of event dispatching thread prevent processing event queue, making application hang.
it's not thread safe, should never try , modify state of ui component out side of edt.
take @ concurrency in swing , worker threads , swingworker more details
public class serversocketworker extends swingworker<void, string> { private jtextarea ta; public serversocketworker(jtextarea ta) { this.ta = ta; } @override protected void process(list<string> chunks) { (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(); //?? } }
to start it, use like...
public void iniciarserver() { serversocketworker worker = new serversocketworker(textareatoappendto); worker.execute(); }
as example
Comments
Post a Comment