0

Trying to write a multithreaded java program but running into some issues, my primary multithreaded class works fine but if i call it from main it starts all the threads and moves on to the next function, i need it to not move on untill ALL threads are done. read gets passed file paths read(String, String)

           Thread one = new Thread(new Runnable() {
                 public void run()
                 {
                     System.out.println("Starting thread 1");
                     read(expiredOneYear, master1);
                     System.out.println("Finished thread 1");
                 }
            });

            Thread two = new Thread(new Runnable() {
                 public void run()
                 {
                     System.out.println("Starting thread 2");
                     read(expiredOneAndQuarterYear, master2);
                     System.out.println("Finished thread 2");
                 }
            });

            Thread three = new Thread(new Runnable() {
                 public void run()
                 {
                     System.out.println("Starting thread 3");
                     read(expiredOneAndHalfYear , master3);
                     System.out.println("Finished thread 3");
                 }
            });

            Thread four = new Thread(new Runnable() {
                 public void run()
                 {
                     System.out.println("Starting thread 4");
                     read(expiredOneAnd3QuarterYear , master4);
                     System.out.println("Finished thread 4");
                 }
            });

            // start threads
            one.start();
            two.start();
            three.start();
            four.start();

below is what happens in main

CSVcompare.run(threadCount, mode, fileLocation);
CSVpattern.run(fileLocation);

I don't want CSVpattern.run() to start untill ALL threads in CSVcompare.run() have finished, otherwise it won't have certain data ready for CSVpattern.run()

user3058423
  • 69
  • 3
  • 8

1 Answers1

1

Add calls to join() at the end of run. the join() method waits for a thread to finish.

try
{
    one.join();
    two.join();
    three.join();
    four.join();
 }
 catch (InterruptedException e)
 {
     System.out.println("Interrupt Occurred");
     e.printStackTrace();
 }

And if you want to ignore interrupts (probably should at least figure out why it was interrupted but this will work)

boolean done = false;
while (!done)
{
    try
    {
        one.join();
        two.join();
        three.join();
        four.join();
        done = true;
    }
    catch (InterruptedException e)
    {
        // Handle interrupt determine if need to exit.
    }
}

https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()

twain249
  • 5,666
  • 1
  • 21
  • 26
  • so add those below one.run(); i did that and get an error unhandled exception type InterruptedException, I am using Eclipse btw – user3058423 Apr 01 '17 at 00:35
  • @user3058423 Well join can throw an InterruptedException if an interrupt occurs while waiting for the threads to finish. Put the code in a `try/catch` to handle it. I'll edit the answer. – twain249 Apr 01 '17 at 00:40
  • I think the first one of the updated answer works, i did a simple version with code cut out to see if it works and it did, i can't do a full test currently as it actually takes hours to run through the data – user3058423 Apr 01 '17 at 00:54