LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   java: multithreading (https://www.linuxquestions.org/questions/programming-9/java-multithreading-336234/)

ashirazi 06-22-2005 06:39 PM

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");
                }
        }

}


jailbait 06-22-2005 07:17 PM

"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

ashirazi 06-22-2005 07:52 PM

I dont quite understand what you mean my trap. Could you please explain?

jailbait 06-22-2005 09:30 PM

"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

Looking_Lost 06-23-2005 06:35 AM

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.

jailbait 06-23-2005 09:50 AM

"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


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