Ridzuan Adris
Ridzuan Adris

Reputation: 1272

How to Put Thread into Array

how do we does that because i failed to google it. my attempt to make below statement working also return error. Could someone show me the way.

Thread[] = {Thread(calculateFile)} //wrong


//thread class
class calculateFile implements Runnable {     

}
public void run() {
    //do some stuff
    System.out.println("do some stuff");
}

additional actually i have a group of thread that run concurrently, i have to wait for all the thread to finish running and then run other program after that. i believe part of doing that i have to put all the thread into array first

Upvotes: 1

Views: 157

Answers (2)

Mavi
Mavi

Reputation: 71

If your goal is to wait for all threads to finish their work before proceeding further, you are not forced to put all threads in an array, nor doing so will halt your code until all threads are done. What you need to do is join each thread on your main thread.

Upvotes: 1

FazoM
FazoM

Reputation: 4956

I think you forgot variable name and new keywords.

Try something like this: Thread[] myThreadArray = {new Thread(new CalculateFile())};

Also your calculateFile class has incorrect brackets, try this:

//thread class
class CalculateFile implements Runnable {     

    public void run() {
        //do some stuff
        System.out.println("do some stuff");
    }
}

PS: good convention is to start class names with capital letter.

Upvotes: 3

Related Questions