I have create a GUI based app using java based on MVC . the major task of my app is to get some data from internet and starts to parsing it.
for getting data from internet I use following class :
public class WebReader implements Runnable {
WebRecordResult WRResult = new WebRecordResult ();
private void getData(args){
.
.
.
setResult(result);
}
public void run() {
getData(args);
WRResult.setResult(getResult());
}
}
public class WebReaderResult {
private AtomicReference<String> result = new AtomicReference<>("");
public String getResult() {
return result.get();
}
public void setResult(String s) {
result.set(s);
}
}
and inside my controller I have created something like :
WebReaderResult wrr = new WebReaderResult();
ExecutorService executor = Executors.newSingleThreadExecutor();
WebReader wr =new WebReader(args);
Future<?> f1 = executor.submit(wr);
executor.shutdown();
try {
f1.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
wrr.getResult();
still after using this code my GUI gets freezed until data becomes available. I don’t want my GUI to be freezed. what shoud I do?
Note : my GUI is swing
The line
blocks until your thread is done executing. If you execute this line in your UI thread, the UI will freeze, of course. But what are you doing with the result anyway? Without more concrete code, it is hard to give a proper solution to your problem.
** EDIT **
Your controller should execute and handle tasks in it’s own thread, and should be completely separated from the UI (for a proper MVC model). Since there are missing information in your code (
args, etc.), an example could be :Results
Task
Controller
UI Code
(in your ActionListener, or any other UI event method)
** EDIT 2 **
As mentioned by other users, since Java 1.6 there is a class called
SwingWorkerthat you may use to do exactly that, but with a lot less code. Just put something like this wherever required in your UI event (or create a separate class to implement the methods) :This last edit is not as clear as the first example, however the
SwingWorkerclass does offer what you are looking for. Now, all you need is to delegate the processing done in bothdoInBackgroundanddoneusing yourWebReaderclass (ie. by callingwr.run();in thedoInBackground, or something.). In any case, I believe you have plenty to go on and about with this now.