Main thread is hanging while 2 child threads running??
Praveen Kumar Jayaram
Ranch Hand
Joined: Jul 14, 2008
Posts: 54
posted
0
Hi,
I am having a progress bar in my swing application. Data to update the progress bar comes from an input stream which is got from a process.getInputStream().
I have a Thread which reads in data(from the process.getInputStream()) into a variable . And this variable is accessed by another Thread to update the progress bar seamlessly.
Totally I have 3 threads. The main thread creates those 2 threads and hangs... It seems the other 2 threads are taking full time to read and update the progress bar. Even using Thread.yield() is not helping.
I have shared my code sample:
Thread to read the input stream: ======================
public class StreamGobbler implements Runnable { InputStream input = null; String pid = null; String solver = null;
Thread to update the progress bar continuously: ===================================
public class ProgressBarIncrementer implements Runnable {
//run method, this will be called implicitly by start() public void run() { File deleteFile = null; try { String inputFilePath = delia.getoutputFileName(); int last = inputFilePath.lastIndexOf("\\"); String str = inputFilePath.substring(0, last + 1);
//File deleteFile = new File(str+"solver.res"); if(Delia.getProcessorType() == 'T'){ deleteFile = new File("TIP3DSolver.res"); } else if(Delia.getProcessorType() == 'S'){ deleteFile = new File("SPLITVISSolver.res"); }
int NMAX = Integer.parseInt(getDeliaInstance().getFlowConditionsInstance().NMAX); while(true) { int pc = ((Integer.parseInt(iteration)*100)/NMAX); progressBar.setValue(pc); //Set value progressBar.setStringPainted(true); progressBar.setString(String.valueOf(pc) + "%"); progressBar.repaint(); //Refresh graphics Thread.yield(); if (deleteFile.exists()) { //if file exists stop the thread break; } } } catch (Exception err) { return; } finally { deleteFile = null; } } }
Main thread part: ============
String cmd[] = new String[]{"cmd", "/C","DentonSolver.bat"}; fortranExecutor = Runtime.getRuntime().exec(cmd);
//create a thread to update progress bar inc = new Thread(new ProgressBarIncrementer()); //set to maximum priority inc.setPriority(Thread.MAX_PRIORITY); //start the thread inc.start();
//Create another thread for reading the input stream to update the progress bar from solver process streamGobbler = new Thread(new StreamGobbler(fortranExecutor.getInputStream(), strPid, "tip3dpc202.exe"));
I'm not sure what would cause the main thread to hang but the ProgressBarIncrementer isn't going to work like that - you're updating the progress bar (a Swing component) from a thread other than the event thread - you're going to need to use SwingUtilities.invokeLater() to update the component on the event thread.
As far as the main thread hanging - I'm not sure if any synchronized methods are being called, or if it's really that the other threads take up all the processor time - have you tried changing the Thread.yield() to Thread.sleep(500) to see if this affects it?
-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.