Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help with non-blocking thread that runs an external process

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help with non-blocking thread that runs an external process

    I have created a Java GUI application which functions as a wrapper for many low level external processes. The utility works as is, but is in desperate need of one major improvement.

    I want my external process run in a non-blocking manner which would permit me to service additional requests in parallel. In a nutshell I want to be able to process data from the external process as the data is being generated. But it appears my basic attempt to check and see if the external process is still running is blocking.

    Below is an excerpt from my ExternalProcess class. Please see inline comments for specific Java functionality questions about threading and blocking.

    	public void Execute()
    	{
    		System.out.println("Starting thread ...\n");
    		Runner = new Thread(this, "ExternalProcessTest");
    		Runner.run();
    		System.out.println("Ending thread ...\n");
    	}
     
    	public void run() 
    	{
    		System.out.println("In run method ...\n");  // Debug purposes only. 
    			// Show that we are in the run loop.
    		try
    		{
    			// Execute string command FullCommand as background process ...
    			Process = Runtime.getRuntime().exec(FullCommand);
    			while(IsRunning())
    			{
    				// External process generates file IO.  I want to process these
    				// files inside this loop.  For the purpose of this demo I have
    				// removed all file processing to eliminate it as the cause
    				// of blocking.  THIS ROUTINE STILL BLOCKS!
    				Thread.sleep(1000);
    			}
    		}
    		catch(Exception e)
    		{
    			System.out.println(e);
    		}
    		System.out.println("Exiting run method ...\n");  // Debug purposes only.
    			// Show that we are exiting the run loop.
    	}
     
    	// With Process (instantiated from Runtime.getRuntime().execute) you get
    	// fire-and-forget backgrounding (non-blocking) or you can wait for 
    	// the process to finish using the waitFor() method (blocking).  I want to
    	// (1) be able to execute a non-blocking external process, (2) that I can 
    	// monitor via threading so as to process data files generated by the 
    	// external process real time.  To facilitate this goal, I have created an 
    	// isRunning() method that uses the exitValue() method.  If the process is 
    	// still running, a call to exitValue() will throw an 
    	// IllegalThreadStateException exception. So I simply catch this execption 
    	// to test if the background process is finished -- at which point I can 
    	// stop processing file IO from the process.  Is this the source of the 
    	// blocking?  If so, is there another way to do this?
    	public boolean IsRunning()
    	{
    		boolean isRunning = false;
    		try
    		{
    			int exitVal = Process.exitValue();
    		}
    		catch(IllegalThreadStateException e)
    		{
    			isRunning = true;
    		}
    		return isRunning;
    	}

    Any of you Java experts out there know how to do this correctly? I'm stumped.

    Thanks in advance.


  2. #2
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Re: Help with non-blocking thread that runs an external process

    Issue has been resolved. I did Runner.run() instead of Runner.start(). Doh!

Similar Threads

  1. Blocking format (important)
    By benglish in forum Java Theory & Questions
    Replies: 3
    Last Post: April 11th, 2011, 06:34 PM
  2. Question about blocking queue
    By Kerr in forum Threads
    Replies: 2
    Last Post: April 8th, 2011, 04:59 AM
  3. Blackjack program runs excrutiatingly slow.
    By sina12345 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 10th, 2010, 04:55 AM
  4. Thread runs only once and not again
    By enflation in forum Threads
    Replies: 3
    Last Post: June 9th, 2010, 10:51 AM
  5. runs once only
    By silverspoon34 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 21st, 2009, 03:31 AM

Tags for this Thread