Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

when/why would a IllegalThreadStateException be thrown?

Posted on 2006-11-16
6
Medium Priority
?
419 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 400 total points
ID: 17960654
should do
0
 

Author Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

719 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