Blanca Hdez
Blanca Hdez

Reputation: 3563

Java multi thread problem .join()

I have checked different questions in this and in other forums, but I didn't find the solution to my problem.

I have an application which runs ffmpeg and exiftool processes. I have concurrency probles, and I would like to control them, using Thread. This is how I built it:

ExiftoolThread

public class ExiftoolThread extends Thread{

    String file;

    public ExiftoolThread(String file){
        this.file = file;
    }
     public void run(){
                serviceToExiftool(file);//Create metadata file
    }
}

FfmpegThread

public class FfmpegThread extends Thread{

    String itemName;

    public FfmpegThread(String itemName){
        this.itemName = itemName;
    }
     public void run(){
         serviceFFmpeg(itemName);//Create thumbnai froma  video
    }
}

Main call

Thread exiftoolThread = new ExiftoolThread(file.getName());
        exiftoolThread.run();
        try {
            exiftoolThread.join(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        log.write("JSON file created.It contains the metadata. ");


            Thread ffmpegThread = new FfmpegThread(itemName);
            ffmpegThread.run();
            try {
                ffmpegThread.join(3000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            log.write("Thumbnail created successfully. ");

After this call finishes, there are more actions working with the results of these processes, and there is always one of them which is missed. I guess that's because one of them (exiftool or ffmpeg) finishes earlier and then the process continue before the other finishes.

I am using ffmpegThread.join(3000); to skip this problem, as the documentation says, this method waits untill the thread is died. What am I missing?

Thanks in advance

Upvotes: 0

Views: 672

Answers (2)

Kien Truong
Kien Truong

Reputation: 11381

You have to call start() to run the code in the new thread. Also, join() block the current thread, so you have to call it after you start all your thread.

Upvotes: 1

Barend
Barend

Reputation: 17444

You need to call Thread.start() instead of Thread.run() on your two threads. The start() method actually spins a new thread to execute code concurrently, while the run() method is a normal method that executes in the calling thread like any other.

additionally, you can improve your thread synchronization setup by replacing the join() calls by use of a java.util.concurrent.CountDownLatch. Your main code will look like this:

CountDownLatch latch = new CountDownLatch(2);
Thread ffmpegThread = new FfmpegThread(itemName, latch);
Thread exifToolThread = new ExifToolThread(itemName, latch);
ffmpegThread.start();
exifToolThread.start();

latch.await(); // With optional timeout

Your two helper threads must both call latch.countDown() when they're done.

Upvotes: 3

Related Questions