Separate hashset for launching a list in multiple threads

I googled and search here for this question and did not find anything similar to what I am looking for.

I populated a HashSet with several objects called Person, I need to set up four or five threads to look for this Person in huge text, the thread seems to be the best solution for better use with hardware.

Confidence in how I can separate this HashSet and start 4 threads? I tried to create a new HashSet list and start a new thread with this new hash divided by 4.

This seems to be a good solution, but is there a better way to do this? How can I separate a hashset and send in parts 4 or 5 new threads?

+4
source share
4 answers

You can implement the producer-consumer scheme: ask one thread to read values ​​from the hash set one by one and put them in a queue, which is then processed by several workflows. You can use the ExecutorService class to manage workers.

Change Here you can:

Define a working class:

public class Worker implements Runnable { private Person p; public Worker(Person p) { this.p = p; } public void run() { // search for p } } 

In the main thread:

 ExecutorService s = Executors.newCachedThreadPool(); for(Person p: hashSet) { s.submit(new Worker(p)); } 
+2
source

Access to the HashSet is O (1), so if you split it into multiple threads, it will not work faster. You better try to split the search file, it's expensive. However, if it is efficient enough, one thread will be optimal.

Keep in mind that using all the cores on your computer may mean that your program is slower. If you just want to use the entire processor on your computer, you can create a thread pool that does nothing but use the entire processor on your computer.

+2
source

A few things to consider:

1) You can use the same HashSet, but you will need to synchronize it (wrap calls to it using the synchronized block. But if all you do is look for things in the hash, multithreading will not buy you a lot.

2) If you want to split the HashSet, you can consider the section on key ranges. For example, if you are looking for a name, names starting with AF go into HashSet1, GL HashSet2, etc. Thus, your searches can be completely parallel.

+1
source

You can iterate through a hash set using Iterator. and while the iteration retrieves the value and creates the thread and starts it.

Else

you can use the ExecutorService API, where concurrent tasks can run in parallel.

+1
source

Source: https://habr.com/ru/post/1388132/


All Articles