Can anyone please tell me why the below posted code blocks the main thread?
the loadFile
methods starts a worker thread that loads a huge log file which might takes 10 seconds to be loaded. And when i run the programm, the button responsible for starting the file thread, freezes till the file is loaded and the file thread finishes its work.
Also,in the three lines follow the call of loadFile
method, I am trying to display something like "loading....", and as long as the file is not loaded or its thread has not finished working, i display the same text "Loading....." with extra dot till the file thread/worker thread terminates.
kindly please let me know why the below code blocks the main thread, and how to achieve displaying a text informs the user that the file is still loading with extra dot a the end of the text.
I hope my question is clear.
if ( (this.client != null) && (this.client.isConnected()) ) {
System.out.println("Client is connected to a broker.");
statusarea.append(Log.w(TAG, "preLoadSystemChecks", "Client Connected") + "\n");
loadFile();
StringBuilder sb = new StringBuilder(".");
statusarea.append(Log.i(TAG, "preLoadSystemChecks", "Loading measurements file.") + "\n");
statusarea.append(Log.i(TAG, "preLoadSystemChecks", "Loading" + sb) + "\n");
while ( (this.fileThread != null) && (this.fileThread.getState() != State.TERMINATED) ) {
sb.append(".");
}
LoadFile():
protected void loadFile() throws MqttException {
// TODO Auto-generated method stub
statusarea.append(Log.d(TAG, "loadFile", "File is loaded") + "\n");
if (this.fileThread == null) {
fileThread = new Thread(fileProcessinRunnable, FILE_THREAD);
fileThread.start();
}
}
Your
while
loop is blocking the main thread until the loader thread terminates:(Note that simply appending a dot to a
StringBuilder
isn't going to result in anything being displayed anywhere.)