Aveneon
Aveneon

Reputation: 171

Incrementing Thread Name

I have made a function that takes x amount of parameters. Each parameter, represents a file.

I want every single of these files to be assigned a thread, for counting the words of the files as fast as possible. Currently I have done something that seems to work, however I find myself in trouble of checking the threads as they are all just assigned with the name "t"

It would be nice to somehow increment the name of the threads. The first thread would be t1 and would be assigned to the first file and so on.

for (File file : fileList) {
            final File f = file;
            Thread t = null;
            ThreadGroup test = null;
            Runnable r = new Runnable() {
                public void run() {
                    Scanner fileScan;
                    try {
                        fileScan = new Scanner(f);
                   }
                   catch(FileNotFoundException e){
                       System.out.println("Something went wrong while accessing the file");
                       return;
                    }
                    int words = 0;
                    while (fileScan.hasNext()) {
                        words++;
                        fileScan.next();
                    }
                    System.out.println(f.getName() + ": " + words + " words");
                    System.out.println(Thread.activeCount() + ": ");
                }
            };
            t = new Thread(r);
            t.start();
        }

The threadcount goes up as it is supposed when checking with Thread.activeCount(), but I have no clue how to ever contact them as I have assigned all with the name t, which makes it hard to make yet another thread that shall wait for their output.

I hope my explaination clearified the problem :/

Edit:

The idea is that I will count the amount of words in different files, every file needs to be assigned a thread for itself to speed it up. Other than that, I want one thread waiting for the output from all the other threads ( meaning I will have to wait for them to finish, hence why I would appriciate accessing the name of the threads ).

At the end that last thread that has been waiting will use the collected data for it's own actions before closing the program down.

Upvotes: 1

Views: 644

Answers (1)

aioobe
aioobe

Reputation: 420951

In order to for instance wait for the threads to finish, you need to save references to the threads you create. You could for instance do

List<Thread> threads = new ArrayList<>();

and then do threads.add(t); at the end of the loop.

After that you could wait for them to finish by doing

for (Thread t : threads) {
    t.join();
}

What's problematic however is that there is no way for you to read the result of the threads you've started.

A much better approach to this is to use an ExecutorService and a ThreadPoolExecutor. This way you can submit Callable<Integer> instead of Runnable, and you'll be able to get the result of the word-count.

Here's an outline to get you started:

ExecutorService service = Executors.newFixedThreadPool(numThreads);
List<Future<Integer>> results = new ArrayList<>();
for (File f : fileList) {
    results.add(service.submit(new Callable<Integer>() {
        // ...
    }));
}

for (Future<Integer> result : results) {
    System.out.println("Result: " + result.get());
}

Upvotes: 1

Related Questions