1

I have a method as below:

// Main Class:
processRecords(ArrayList<String> records) {
    Boolean isSuccess = false;

    ProcessRecordThread thread = new ProcessRecordThread(records);
    new Thread(thread).start();
    return isSuccess;
}    

// Thread class:

run() {
    for (String record : records) {
        try {
            process(record);
        } catch (Exception e) {
            isSuccess = false;
        } 
    }
}

I want my main class to wait for my thread to complete processing of all records and then return isSuccess variable and continue with main class execution.

Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
  • 2
    If you want to wait until everything is complete, why do it in a separate thread at all? – Andy Turner Jul 09 '18 at 10:21
  • Possible duplicate of [Java Wait for thread to finish](https://stackoverflow.com/questions/4691533/java-wait-for-thread-to-finish) – am9417 Jul 09 '18 at 10:21
  • 1
    Note: It looks like `ProcessRecordThread` is not a Thread, but an implementation of Runnable. Maybe it should be called `RecordProcessor`? or `RecordProcessRunnable`? Why do you want to run this process in a single separate Thread and wait for its completion in the calling thread? This would only be beneficial if either the calling thread starts multiple threads that do parallel processing, or the calling thread does not wait for the processing to complete (asynchronous processing). – Adriaan Koster Jul 09 '18 at 10:22
  • @AdriaanKoster i have created a new thread because my each record in my list contain multiple records within itself.the process function inside my function is async function. – Akshay Dhonde Jul 09 '18 at 10:26
  • Did you consider using `java.util.concurrent.ExecutorService`? – prasad_ Jul 09 '18 at 10:43
  • Looping all records in parallel is as simple as `records.parallelStream().forEach(ProcessRecordThread::process)` or sth. like that – M.F Jul 09 '18 at 13:01

0 Answers0