Solved

when/why would a IllegalThreadStateException be thrown?

Posted on 2006-11-16
6
411 Views
Last Modified: 2012-06-27
when/why would a IllegalThreadStateException be thrown?  If I try something by mistake on a thread?  Say start it twice?   Would this be thrown if I were to CTRL+Break a program when it was in that section of code?
I am calling a Runtime process call to execute another program within my code.  I then use a doWaitFor method to send the process to and wait for the program to end if necessary.
I am wondering when the IllegalThreadStateException would be thrown and caught....  

Runtime rprogram = Runtime.getRuntime();
Process p = rprogram.exec(command);
                  
InputStream stderr = p.getErrorStream();
                  
                  
InputStream stdin = p.getInputStream();
                  
exitcodes = doWaitFor(p);




private static int doWaitFor(Process p) {
            
            int exitValue = -1;  // returned to caller when p is finished
            
            try {
                  
            InputStream in  = p.getInputStream();
            InputStream err = p.getErrorStream();
                  
            boolean finished = false; // Set to true when p is finished
                  
                  while( !finished) {
                        try {
                        String input="";
                        String error="";
                        while( in.available() > 0)
                              {
                                    
                            // Print the output of our system call
            Character c = new Character( (char) in.read());
            input=input+c;
                              }
                              
                              
                              
                              
            while( err.available() > 0)
                  {
                                    
            // Print the output of our system call
            Character c = new Character( (char) err.read());
                                    
                  error=error+c;
                  }
                              
                              
                              
                  exitValue = p.exitValue();
                  finished  = true;
                              
                        }
                  catch (IllegalThreadStateException e)
                                               {
                              
                  / Process is not finished yet;
                  // Sleep a little to save on CPU cycles
                  Thread.currentThread().sleep(500);
                  }
            }
                  
                  
            }
            catch (Exception e)
                               {
                  
            // unexpected exception!  print it out for debugging...
                                    
            }
            
            // return completion status to caller
            return exitValue;
}

Thanks
ryno71
0
Comment
Question by:ryno71
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 17960080
that will be thrown if you try and get the exit value before the process has terminated.
0
 

Author Comment

by:ryno71
ID: 17960199
Say by ending the process early?  What steps would you recommend that I take to account for that?

0
 
LVL 92

Expert Comment

by:objects
ID: 17960255
call waitFor() before checking the exitValue
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ryno71
ID: 17960628
while searching I did find that there was an old bug for waitFor(), which is why I tried using the other method above

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4254231

would waitfor() handle a ctrl+ brk type of scenario?  if not, what should I look into?

thanks
ryno71
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 17960654
should do
0
 

Author Comment

by:ryno71
ID: 18039920
Sorry finally got a vacation, thanks
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question