How do I slow down the generation of Threads in java?

Go To StackoverFlow.com

2

I am spawning 20 threads (they are all supposed to be running at the same time). However, they are all created at the same time and all start running at the same time, which gives the entire program major lag (this is lag, my computer is not slow).

So, I want to have them created at different times, e.g. generate one every 2 seconds or so. How do I do this? I have tried using a ton of stuff but none of it works the way I want it to. I have tried using boolean methods to not loop again until it is true, but this doesn't seem to work. Any ideas?

for (int i = 0; i < 20; i++) {
    Rain r = new Rain();
    r.start();
}
2012-04-04 20:11
by jackcogdill
I can create 1000s of threads on my Macbook Pro in a second. Can you explain a bit more what you mean by lag and why you think thread creation is at fault - Gray 2012-04-04 20:12
are you messing with thread priorities or using lots of memory in the threads themselves? 20 threads should be no issue for anything, unless you have a lot of contention between the threads.. - John Gardner 2012-04-04 20:16
20 threads, or 150 threads, takes an imperceptible amount of time. If there is something else going on in the thread ctor tht causes increased delay, then a reasonable solution to 'lag' is to create one low-priority thread that then creates the 20/150 other threads at a low piority. I've never seen any need for any Sleep(2000), in a thread construction loop, ever - Martin James 2012-04-04 21:02


4

Try running the thread generator as a thread, then implement a Thread.sleep(2000)

2012-04-04 20:13
by kevingreen
thanks, i'll go try tha - jackcogdill 2012-04-04 20:13
I would add 0.5 point if I could. Using a generator thread, preferably at a low priority, that creates 20/150 low priority threads is a good idea. The sleep() is just not - Martin James 2012-04-04 21:03
Have a point anyway : - Martin James 2012-04-04 21:04


3

You could probably just create a spawner thread, which just sleeps 2 seconds between spawning each thread:

for (int i = 0; i < 20; i++) {
    Rain r = new Rain();
    r.start();
    Thread.sleep(2000);
}

Note: Left out try-catch blocks

2012-04-04 20:16
by Henrik Karlsson
Don't encourage this sleeping - Martin James 2012-04-04 21:04
@MartinJames Why should I not use sleep here? The bad reputation of the sleep method is mainly due to the misuse of it. See this question: http://stackoverflow.com/questions/3956512/java-performance-issue-with-thread-slee - Henrik Karlsson 2013-04-03 16:25


2

If you are experiencing lag due to the number of threads you are creating, the best solution is probably to lower the number of threads you are creating.

Also, Java 5 introduced the executor service framework, Which was improved again in Java 7 with fork/joins, so you shouldn't have to be creating threads yourself at all under normal circumstances. (Here is a link to a page with a pretty good explanation of those concepts.)

I generally don't start more threads than I have cores on the machine, like so:

int availableThreads = Runtime.getRuntime().availableProcessors();
ExecutorService executorService = Executors.newFixedThreadPool(availableThreads);

// ...Create List<Future<ReturnObject>>
// populate list by calling futures.add(executorService.submit(callable));

executorService.shutdown();

This is because, as long as the process in computationally intensive, your biggest threading gains are made when you have simultaneous computation on each core instead of thread switching on a single core. Of course, that changes when you're talking about a process that's disk or network intensive, but it's a pretty good rule of thumb.

2012-04-05 00:30
by Tim Pote


1

I don't really see why it will be lagged for only 20 threads creating but you can put a sleep if you want them to be started at different time:

for (int i = 0; i < 20; i++) {
    Rain r = new Rain();
    r.start();
    Thread.sleep(2000);
}
2012-04-04 20:14
by evanwong
ok, it totally works now thanks!

(btw, it was like 150 threads - jackcogdill 2012-04-04 20:20

Ads