How many threads run in Java?

I have this wonderful idea to speed up the time required to generate 36 files: use 36 threads! Unfortunately, if I start a connection (a j2ssh connection object) with 36 threads / sessions, then everything is much more than if I execute each thread at once

So what should I do? How do I find the best number of threads I should use? Because before starting my 36 threads, thread Activecount() is 3? I'm using Lenovo laptop Intel Core i5

Solution

You can use executorservice to reduce it to a more reasonable number of threads You may want to use something close to the number of available processor cores, such as:

int threads = Runtime.getRuntime().availableProcessors();
ExecutorService service = Executors.newFixedThreadPool(threads);
for (int i = 0; i < 36; i++) {
    service.execute(new Runnable() {
        public void run() {
            // do what you need per file here
        }
    });
}
service.shutdown();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>