Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
}
like image 207
jackcogdill Avatar asked Dec 22 '25 11:12

jackcogdill


1 Answers

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

like image 117
kevingreen Avatar answered Dec 24 '25 00:12

kevingreen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!