Grand Masta1
Grand Masta1

Reputation: 837

How do you fix a Thread Join() that is stuck

I am new to multi-threading and I am simply getting hands on practice by trying to print a 14000 size String Arraylist using threads. I am not considering efficiency nor even usefulness for that matter; I just want to understand the threading concept.

My output has been extremely faulty and I have tried all kinds of synchronizes and other failed attempts. I came across a tool called join at the moment, however my program doesn't exit and stays running. I debugged the code and the program simply gets stuck on the first thread join and I can't think of any reason or solution to remedy the problem.

The way I approached the problem is by creating 4 threads to split the task of the printing.Then I would wait until all threads were complete and use the join right before closing the output file.

public class Threadprinting implements Runnable{
private static List<String> list;
private Thread T1,T2,T3,T4;
public void printinput(List<String> store){
    list=new ArrayList<String>(store);
    Threadprinting threadprinting=new Threadprinting();
    Threadprinting.generatethreads();   
}

public void generatethreads() {
    T1=new Thread(this,"t1");
    T1.start();
    T2=new Thread(this,"t2");
    T2.start();
    T3=new Thread(this,"t3");
    T3.start();
    T4=new Thread(this,"t4");
    T4.start();
}

public void run(){
    try {               
        PrintWriter out = new PrintWriter(new FileWriter("Output.txt"));
        switch (Thread.currentThread().getName()) {
            case "t1":
                System.out.println("Thread "+ Thread.currentThread().getName() + " is running");
                for (int i = 0; i < list.size() / 4; i++) {
                    out.println((list.get(i)));
                }
                break;
            case "t2":
                System.out.println("Thread "+ Thread.currentThread().getName() + " is running");
                for (int i = list.size() / 4; i < list.size() / 2; i++) {
                    out.println((list.get(i)));
                }
                break;
            case "t3": 
                System.out.println("Thread "+Thread.currentThread().getName()+" is running");
                for (int i = list.size()/2; i < list.size()/4+list.size()/2; i++) {
                    out.println((list.get(i)));
                }
                break;
            case "t4": 
                System.out.println("Thread "+Thread.currentThread().getName()+" is running");
                for (int i = list.size()/4+list.size()/2; i < list.size(); i++) {
                    out.println((list.get(i)));
                }
                break;
            default:
                System.out.println("filler");
                break;
            }
            jointhreads();
            out.close();
    }
    catch(IOException e){
        System.out.println("problem came from here");
    }
}

Specifically:

private void jointhreads() {
    try {
        T1.join();
        T2.join();
        T3.join();
        T4.join();
    } catch (InterruptedException e) {
        System.out.println("the joins failed");
    }
}

}

The program fails to terminate and stays running at T1.join according to the debugger.

Upvotes: 3

Views: 3836

Answers (2)

ben75
ben75

Reputation: 28726

The statement

T1.join();

means : "the current thread must wait until T1 terminates". Your problem is that the current thread is T1.

So, it's kind of I suspend my run until I reach the end. (i.e. deadlock)

Upvotes: 1

Antoniossss
Antoniossss

Reputation: 32535

You are invoking jointhreads inside thread you want to join, so T1 is basicly waiting for T1 (itself) to terminate but that will never happen.

You should invoke jointrheads in your main thread, so the application will wait until all worker threads will finish their job

Upvotes: 7

Related Questions