Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Long-term task execution
Index -> Programming, Java -> Java Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Zeroth




PostPosted: 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?
Sponsor
Sponsor
Sponsor
sponsor
btiffin




PostPosted: 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?
Zeroth




PostPosted: Tue Jul 08, 2008 1:34 pm   Post subject: Re: Long-term task execution

as part of the thread wrapper?
btiffin




PostPosted: 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
Zeroth




PostPosted: 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?
rizzix




PostPosted: 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.
Zeroth




PostPosted: 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?
rizzix




PostPosted: 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\\"));
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: 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.
Zeroth




PostPosted: 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.
rizzix




PostPosted: 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.
btiffin




PostPosted: 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?
Zeroth




PostPosted: Tue Jul 08, 2008 6:27 pm   Post subject: Re: Long-term task execution

Its likely to be what Rizzix is saying.
btiffin




PostPosted: 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
Zeroth




PostPosted: 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
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 19 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: