Computer Science Canada

Long-term task execution

Author:  Zeroth [ 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?

Author:  btiffin [ Tue Jul 08, 2008 1:20 pm ]
Post subject:  RE:Long-term task execution

Umm, you do have a .run() method as part of this wrapper?

As per http://java.sun.com/docs/books/tutorial/essential/concurrency/runthread.html

Cheers?

Author:  Zeroth [ Tue Jul 08, 2008 1:34 pm ]
Post subject:  Re: Long-term task execution

as part of the thread wrapper?

Author:  btiffin [ Tue Jul 08, 2008 1:46 pm ]
Post subject:  RE:Long-term task execution

Yeah, thread.start() needs a real run(). It doesn't kick start main. run()'s job is to kickstart the main.

Class Thread's default run method is an empty do nothing method.

That one pager doc from Sun explains far better than I ever could.

Cheers

P.S. This could be a way deeper problem, but I get the sneaking suspicion you may go "DOH!". Smile

Author:  Zeroth [ Tue Jul 08, 2008 3:15 pm ]
Post subject:  Re: Long-term task execution

code:

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;
                        }
                       
                       
                       
                        SwingUtilities.invokeLater(new Runnable() {
                                public void run(){
                                        JOptionPane.showMessageDialog(parent, "Finished!");
                                        Parser parser  = new Parser(parent.parent);
                                        try{
                                                Document doc = parser.parse(new File("D:\\kegg\\seq_alignments\\Release\\results.txt"));
                                                parent.parent.textArea.setDocument(doc);
                                        }
                                        catch (BadLocationException ble){
                                               
                                        }
                                        catch (IOException ie){
                                               
                                        }
                                }
                        });          

        }
        }

I'd like to point out that when a Thread is constructed with a Runnable, the run method actually just calls the runnables run() method.

Pretty much, when seg_alignnebrs.exe is called, it just sits there doing nothing. Why?

Author:  rizzix [ Tue Jul 08, 2008 4:48 pm ]
Post subject:  RE:Long-term task execution

You're not supposed to directly call the .run() method yourself, you call the .start() method instead. Anyway, not sure why it would just sit there.

Author:  Zeroth [ Tue Jul 08, 2008 4:51 pm ]
Post subject:  Re: Long-term task execution

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?

Author:  rizzix [ Tue Jul 08, 2008 4:57 pm ]
Post subject:  RE:Long-term task execution

Actually, that's because it's not running your process. (Since `command` is null)

Perhaps you meant:
code:
shell.exec("D:\\kegg\\seq_alignments\\Release\\seq_alignnebrs.exe", a , new File("D:\\kegg\\seq_alignments\\Release\\"));

And perhaps the above can be simplified to:
code:
shell.exec("seq_alignnebrs.exe", a , new File("D:\\kegg\\seq_alignments\\Release\\"));

Author:  rizzix [ Tue Jul 08, 2008 5:05 pm ]
Post subject:  RE:Long-term task execution

Nvm, didn't see the code above. However yea just make sure you're passing the full path to the .exe.

Author:  Zeroth [ Tue Jul 08, 2008 5:12 pm ]
Post subject:  Re: Long-term task execution

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.

Author:  rizzix [ Tue Jul 08, 2008 5:16 pm ]
Post subject:  RE:Long-term task execution

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.
Which seems to be what you're facing.

Author:  btiffin [ Tue Jul 08, 2008 5:46 pm ]
Post subject:  Re: Long-term task execution

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?

Cheers?

Author:  Zeroth [ Tue Jul 08, 2008 6:27 pm ]
Post subject:  Re: Long-term task execution

Its likely to be what Rizzix is saying.

Author:  btiffin [ Tue Jul 08, 2008 6:40 pm ]
Post subject:  RE:Long-term task execution

Agreed.
I really shouldn't have piped up on this one. I felt like I was asking a guru; "Is it plugged in?" Smile

Cheers

Author:  Zeroth [ Tue Jul 08, 2008 9:58 pm ]
Post subject:  Re: Long-term task execution

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. Wink

Author:  Zeroth [ Wed Jul 09, 2008 11:41 am ]
Post subject:  Re: Long-term task execution

Okay, so now the question is, and forgive my ignorance on these matters, how do I set up a pipe or stream from the executed process? Theres lots(tonnes) of documentation on reading and writing through pipes and streams between different Java threads.

Author:  rizzix [ Wed Jul 09, 2008 11:59 am ]
Post subject:  RE:Long-term task execution

Process p = ...;

p.getInputStream();

p.getOutputStream();

Author:  Zeroth [ Wed Jul 09, 2008 12:09 pm ]
Post subject:  Re: Long-term task execution

... wow, thanks Rizzix!

Author:  Zeroth [ Wed Jul 09, 2008 1:13 pm ]
Post subject:  Re: Long-term task execution

Dude, that worked. Thanks Rizzix!

Bits and karma donated


: