*
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
Author

Main thread is hanging while 2 child threads running??

Praveen Kumar Jayaram
Ranch Hand

Joined: Jul 14, 2008
Posts: 56
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;

StreamGobbler(InputStream in, String pid, String solver)
{
input = in;
this.pid = pid;
this.solver = solver;
}
public void run()
{
try
{
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);
try
{
scan = new Scanner(line);
Thread.yield();
if(scan.hasNext())
{
int iter = Integer.parseInt(scan.next());
iteration = String.valueOf(iter).trim();
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
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.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);

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);
return;
}

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

streamGobbler.start();


Praveen
Nathan Pruett
Bartender

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?


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
Don't get me started about those stupid light bulbs.
 
subject: Main thread is hanging while 2 child threads running??