-1

I have this code, and I need condition to know if the thread "th1" was finished because I need execute some operation after this thread.. such as I need print message when finishing from this thread inside the main function..

public static void main(String[] args) {

    File folder=new File("E:/project_3/audio/");
    File[] fileList=folder.listFiles();
    for ( File file:fileList) {

        if(file.isFile()){
            System.out.println(file.getName());
            thread_reading th1=new thread_reading(file.getName());
            new Thread(th1).start();
        }
    }

}
trante
  • 33,518
  • 47
  • 192
  • 272
  • If your flow pauses for a thread completion, why do you need to create thread instead of simply executing same logic on main thread? – kosa Apr 16 '15 at 19:18
  • 1
    https://docs.oracle.com/javase/tutorial/essential/concurrency/join.html – MadConan Apr 16 '15 at 19:20
  • There is a very nice answer to your question here http://stackoverflow.com/questions/702415/how-to-know-if-other-threads-have-finished – Sandeep Poonia Apr 16 '15 at 19:22
  • Could you share the code for `thread_reading`? If it's a `Runnable` then you can do `System.out` at the end of its `run` method. – Mick Mnemonic Apr 16 '15 at 19:23
  • Use a Future if you want completion semantics on a thread. –  Apr 16 '15 at 19:29

3 Answers3

4

I think you can use join :

Thread th1 = new Thread(th1);
th1.start();
... more code...
th1.join();

https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join%28long%29

Daniel Langdon
  • 5,899
  • 4
  • 28
  • 48
dpolivaev
  • 494
  • 3
  • 12
  • With this solutions, there is no point in using threads, as the processing of the files will be serialized. Technically valid, but quite useless :) – Olivier Croisier Apr 16 '15 at 19:25
  • Not true, you can do lots of work between start() and join() in the current thread, in parallel. – Daniel Langdon Apr 16 '15 at 19:29
  • If you call `start()` then immediately `join()` inside the loop, the processes will be serialized. For your solution to work, you would need to store all threads in some list, start them all, *then* loop on them to call `join()`. – Olivier Croisier Apr 16 '15 at 19:40
0

In order to maximize the parallelization of the processing of your audio files, I would split this algorithm in two :

  • First, read the directory to find the files to process, and store them in a List or array. This would allow to know exactly how many files must be processed (useful for the technical suggestion below).
  • Then, for each file found, start a thread to process it with the thread_reading job.

To be notified when all files have been processed, I would use a CountDownLatch. Each thread would call countdown() to tell it has finished ; and the main thread would simply have to wait for those N completion signals.

Here is the main code :

// 1. Gather files
//File folder = new File("E:/project_3/audio/");
File[] fileList = folder.listFiles(new FileFilter() {
    @Override
    public boolean accept(File pathname) {
        return pathname.isFile();
    }
});

// 2. Process the files in parallel
CountDownLatch completionCounter = new CountDownLatch(fileList.length);
for (File file : fileList) {
    System.out.println(file.getName());
    thread_reading th1 = new thread_reading(file.getName(), completionCounter);
    new Thread(th1).start();
}

// 3. Wait for all processes to finish
try {
    completionCounter.await();
} catch (InterruptedException e) {
    e.printStackTrace();
}

And here is the code for the thread_reading job :

public class thread_reading implements Runnable {

    private final String name;
    private final CountDownLatch completionCounter;

    public thread_reading(String name, CountDownLatch completionCounter) {
        this.name = name;
        this.completionCounter = completionCounter;
    }

    @Override
    public void run() {
        // ... do stuff ...
        System.out.println(name);

        // Say it's done
        completionCounter.countDown();
    }
}
Olivier Croisier
  • 6,139
  • 25
  • 34
0

You could try creating an executor and adding your runnables there and wait for them to terminate

//Set how many threads you want to run in parallel
ExecutorService executor = Executors.newFixedThreadPool(5);
for (File file: fileList) {
  if (file.isFile()) {
    System.out.println(file.getName());
    thread_reading th1 = new thread_reading(file.getName());
    executor.submit(th1);
  }
}
executor.shutdown();
try {
  executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
  //wait
}
gfelisberto
  • 1,655
  • 11
  • 18