Solved

when/why would a IllegalThreadStateException be thrown?

Posted on 2006-11-16
6
401 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
Comment Utility
that will be thrown if you try and get the exit value before the process has terminated.
0
 

Author Comment

by:ryno71
Comment Utility
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
Comment Utility
call waitFor() before checking the exitValue
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:ryno71
Comment Utility
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
Comment Utility
should do
0
 

Author Comment

by:ryno71
Comment Utility
Sorry finally got a vacation, thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now