Solved

when/why would a IllegalThreadStateException be thrown?

Posted on 2006-11-16
6
405 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java array passed to SQL where clause 23 64
Java Timer (static) 9 36
factorial example 4 35
hibernate jars 4 11
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

863 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

20 Experts available now in Live!

Get 1:1 Help Now