简体   繁体   中英

Java - Manage large number of threads versus multicore

I am currently dealing with a script which make some checks & insertions for each databases of a Mongo server. It's multithreading.

for (int i = 0; i < countDatabase; i++) {     
    new threadDatabase(database.get(i).toString()).start();            
}

Problem: I have 16 databases to parse...I am afraid the PC where the script will be set up, won't manage 16 (or more) threads...

Any ideas how to deal a large number of threads? I heard about pool, but not sure it can deal with database name I send as parameter...

Thanks

16 threads isn't a lot of threads. Sure, it's probably more threads than you've got CPU cores, but if you're talking to a remote database, odds are you'll spend most of the time waiting for I/O to complete so CPU won't be a limiting factor.

Apart from that, a thread pool would work just fine for you:

ExecutorService executorService = ...;
for (int i = 0; i < countDatabase; i++) {     
    final String dbName = database.get(i).toString();
    executorService.submit(new Runnable(){
        @Override
        public void run() {
            parseDatabase(dbName);
        }
    });
}

16 threads on a PC?

Looking at my Windows Task manager/Performance, it shows 1238 threads running. It's not even breaking a sweat. Top process is sidebar.exe - 66 threads.

OK, so you're running some other U**xy OS - 16 threads not likely to be a problem there either.

16 threads is nothing. If they are all CPU-intensive, the box will get loaded up but, hey, that's the point!

Try a pool if you have to, maybe to try and improve performance, but don't do it just because you are afraid that 16 threads is too much for your OS.

Rgds, Martin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM