Posted: Tue Jul 08, 2008 11:52 am Post subject: Long-term task execution
I'm building a gui, in Swing. There is a long-running task that needs to be called out to, in an .exe, for which I'm using Runtime.exec.
The problem however, is that when I place the call in a subclass of Runnable, and then wrap it in a thread, and call thread.start... the task does get called, but it sits at, well, sleeping, and doesn't do anything. Why is this? What can I do to change this?
AlignRunner a = new AlignRunner();
a.parent = parent;
a.command = command;
Thread t = new Thread(a);
t.run();
try{
parent.parent.doc.insertString(0, "Processing alignment, please wait.", new SimpleAttributeSet());
}
catch (BadLocationException ble){
}
code:
private class AlignRunner implements Runnable{
public TasksPanel parent;
public String[] command;
public void run() {
// TODO Auto-generated method stub
try{
Runtime shell = java.lang.Runtime.getRuntime();
String[] a=null;
File f= new File("D:\\kegg\\seq_alignments\\Release\\seq_alignnebrs.exe");
if (f.exists()){
JOptionPane.showMessageDialog(parent, "File does exist.");
Process waitThread=shell.exec(command, a , new File("D:\\kegg\\seq_alignments\\Release\\"));
try {waitThread.waitFor();}
catch(InterruptedException ie){
JOptionPane.showMessageDialog(parent, "Task could not complete.");
return;
}
}
}
catch (IOException ie){
ie.printStackTrace();
JOptionPane.showMessageDialog(parent, "Could not write or read from disk. Please check your computer.");
return;
}
I know! However, nothing happens when I do that. So, I call Thread.run and stuff actually happens. However, the program being called does not do anything. Its supposed to though. It just sits there... sleeping. Thats all I need answered. WHY is it doing that? What can I do about it?
No no. It starts the process. And DOES NOTHING. I watch it on the task manager. It does nothing. Sorry if I sound irritable, but I'm just at my wits end here getting this to work. If you look at the first code snippet, you'll see where I'm assigning command to the class variable command. Thats how it executes that data. But the problem is the executed process isn't doing anything. I do the exact same command via the dos shell, and it works. My question is: what could cause a command, sent via a shell/pipe whatever, from actually doing anything? Does it have to do with thread scheduling? Am I using the wrong function?
Here, imagine you typed grep blah, and you sat there waiting for it to finish. Maybe you expect it to take awhile, and know that its memory usage will grow, as will the cpu usage(to about 25%). You check it on the task manager, say, top, and see it sitting there and doing NOTHING. Its started... yet is not doing what its supposed to. Thats whats happening here.
Okay, I'm going to guess here but, you might have to "read" all the output of the process to have it proceed. Further, to quote the docs
Quote:
Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.
Ok, if your run() method isn't empty, look for blocking IO. I've not loaded or tried the code, but I did notice a ShowMessageDialog. Does that block? I'm not a Java guru by any means. You may have a small modal dialog box hidden beneath another window?
Hehe, even the best of us suffer days of tearing our hair out... over stupid stupid stupid mistakes. For example, I spent six hours trying to get HTMLStyledDocuments to accept color... when I should have just used html. *head bonk* It happens to the best of us. Even me.