File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Main thread is hanging while 2 child threads running?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Main thread is hanging while 2 child threads running??" Watch "Main thread is hanging while 2 child threads running??" New topic

Main thread is hanging while 2 child threads running??

Praveen Kumar Jayaram
Ranch Hand

Joined: Jul 14, 2008
Posts: 56

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;

StreamGobbler(InputStream in, String pid, String solver)
input = in; = pid;
this.solver = solver;
public void run()
InputStreamReader inputReader = new InputStreamReader(input);
BufferedReader br = new BufferedReader(inputReader);
String line=null;

Scanner scan = null;
while ( (line = br.readLine()) != null)
//&& checkPIDOfSolver(pid, solver))
System.out.println(">" + line);
scan = new Scanner(line);
int iter = Integer.parseInt(;
iteration = String.valueOf(iter).trim();
catch(Exception e)
catch(Exception e)

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.setString(String.valueOf(pc) + "%");
progressBar.repaint(); //Refresh graphics
if (deleteFile.exists())
//if file exists stop the thread
} }
catch (Exception err)
deleteFile = null;

Main thread part:

String cmd[] = new String[]{"cmd", "/C","DentonSolver.bat"};
fortranExecutor = Runtime.getRuntime().exec(cmd);

strPid = getPIDOfSolver("tip3dpc202.exe");
if(strPid == null)
DentonLogger.log(Level.SEVERE, "Could not start tip3dpc202.exe");
JOptionPane.showMessageDialog(this.getFrame().getContentPane(), "Could not start tip3dpc202.exe", "Error", JOptionPane.ERROR_MESSAGE);

//create a thread to update progress bar
inc = new Thread(new ProgressBarIncrementer());
//set to maximum priority
//start the thread

//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"));


Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

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?

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: Main thread is hanging while 2 child threads running??
jQuery in Action, 3rd edition