Solved

when/why would a IllegalThreadStateException be thrown?

Posted on 2006-11-16
6
407 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to convert String matching to regex in java 4 51
web application structure 18 102
hibernate insert example 13 34
arguments to jar 5 35
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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 learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

830 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