LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-22-2005, 06:39 PM   #1
ashirazi
Member
 
Registered: Jul 2004
Posts: 60

Rep: Reputation: 15
java: multithreading


Hi,

I was writing a program what uses an external application (ABC converter) to convert an image using the command line feature of java. The program runs fine, but I'm not sure how would I know if the image is converted? I use threads to track the conversion, but that doesnt seem to be helping.

Im my code, the thread displays "done", while the image is still being converted (i can see the process being run on the background). Any ideas?

Thanks
raven

Code:
import java.io.File;
import java.io.IOException;

public class ImageConverter  {
	private static long primaryID;
	public ImageConverter() {
		System.out.println("Contructor");
		Thread thread = new Thread( new OneImageRun("abc \"d:\\img.jpg\" /convert=\"d:\\out_tiff.tiff\"") );
		
		thread.start();
		while( thread.isAlive() ){
			try{
				Thread.sleep(500);
				System.out.println("running");
			}catch(Exception e){System.out.println(e);}
		}
		
		if( !thread.isAlive() ){
			System.out.println("DONE");	
		}
		
		
	}

	public static void main(String str[])  {
		new ImageConverter();
	}

	private class OneImageRun implements Runnable {
		private Runtime runtime;
		private Process process;
		private String location;
		
		public OneImageRun(String _location){
			System.out.println("Contructor one img");
			runtime = Runtime.getRuntime();
			location = _location;
						
			try{
				process = runtime.exec( location );	
			}
			catch(Exception e){System.out.println(e);}
		}

		public void run() {
			System.out.println("started");
		}
	}

}
 
Old 06-22-2005, 07:17 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,334

Rep: Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547
"Im my code, the thread displays "done", while the image is still being converted (i can see the process being run on the background). Any ideas?"

Rather than trying to sleep intermittently until the child process terminates I would trap the SIGCHLD signal generated when the child process terminates. I have never programmed in Java but I suggest you check to see if Java has a trap function. If it does I recommend that you use trap.

-----------------------
Steve Stites
 
Old 06-22-2005, 07:52 PM   #3
ashirazi
Member
 
Registered: Jul 2004
Posts: 60

Original Poster
Rep: Reputation: 15
I dont quite understand what you mean my trap. Could you please explain?
 
Old 06-22-2005, 09:30 PM   #4
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,334

Rep: Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547
"I dont quite understand what you mean my trap. Could you please explain?"

A trap command notifies the kernel that you have a section of code to handle certain signals. The trap command give the type of signal(s) that you want to handle and the code to handle that signal(s). After issuing the trap command your code continues normally. If and when the signal occurs the kernel executes the code specified by the trap command and then continues with the main code wherever it happened to be when the signal occured. If the trap code ends with an exit command then the program terminates regardless of what it was doing when the signal occured.

If you issue man trap you will find the explanation for the script command trap buried deep in the explanation of bash. Here is an explanation of how to use trap to exit when a user types ctrl-C.

http://www.freeos.com/guides/lsst/ch04sec12.html

Several languages support signals and trap functions. I have googled for Java trap and can find nothing meaningful. I also googled for Java signals and did find something meaningful. Here is the first explanation I found of Java signal processing.

http://www.geeksville.com/~kevinh/projects/javasignals/

Here is a list of Linux signals.

http://www.comptechdoc.org/os/linux/...pgsignals.html

You are probably interested in signal 17, SIGCHLD. Linux sends SIGCHLD when a child process terminates.

-----------------------------
Steve Stites
 
Old 06-23-2005, 06:35 AM   #5
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
You could also replace your while loop with


Code:
try{
		thread.join();
	}catch(InterruptedException ie){
							     System.err.println(ie.getMessage());
							   }
thread.join() causes execution to wait until "thread" has finished.
 
Old 06-23-2005, 09:50 AM   #6
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,334

Rep: Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547
"thread.join() causes execution to wait until "thread" has finished."

That is great. That must be the way that Java waits for SIGCHLD.

------------------------
Steve Stites
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Slow multithreading with 2.6.13 andy753421 Linux - Software 8 09-16-2005 04:29 PM
Multithreading jayashrik Programming 1 07-20-2005 04:13 AM
Strange multithreading live_dont_exist Programming 2 05-01-2005 07:26 PM
java multithreading wait for all threads to die true_atlantis Programming 1 10-01-2004 06:26 PM
multithreading libraries Pres Programming 3 06-20-2003 03:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration