multithreading - JAVA - External exe locking when called from launcher -
i have been trying use custom output stream display output executable on jtext area.
the executable called button via
try { process p = runtime.getruntime().exec("cgminer.exe" + " -o " + infos.address + ":" + infos.port + " -u " + infos.user + " -p " + infos.password); p.waitfor(); string line; bufferedreader error = new bufferedreader(new inputstreamreader(p.geterrorstream())); while((line = error.readline()) != null){ system.out.println(line); } error.close(); bufferedreader input = new bufferedreader(new inputstreamreader(p.getinputstream())); while((line=input.readline()) != null){ system.out.println(line); } input.close(); outputstream outputstream = p.getoutputstream(); printstream printstream = new printstream(outputstream); printstream.println(); printstream.flush(); printstream.close(); } catch (exception e) { // ... } } }
and output directed jtext with
public class customoutputstream extends outputstream { private jtextarea textarea; public customoutputstream(jtextarea textarea) { this.textarea = textarea; } @override public void write(int b) throws ioexception { textarea.append(string.valueof((char) b)); textarea.setcaretposition(textarea.getdocument().getlength()); } }
my problem when call first class button locks , no output makes jtext. when force close cgminer output appears.
any appreciated has brain warped.
my problem when call first class button locks , no output makes jtext. when force close cgminer output appears.
this classic symptom of tying swing event thread. when run long-running piece of code on swing event thread (also known edt or event dispatch thread), prevent thread doing chores including painting gui , interacting user, "freezing" gui.
the solution not comment waitfor()
suggested, rather run , other blocking code in backgrounds thread such swingworker. when this, make sure take care update swing gui on event thread. swingworker has built-in way via publish , process methods.
for details on this, please check out concurrency in swing
you need more threading since lines of code blocking , prevent lines down stream running until unblock, important action done. instance these block:
// blocks p.waitfor(); // blocks both times use while((line = error.readline()) != null){ system.out.println(line); }
Comments
Post a Comment