I am using the Java Thread class and given a user input parameter k
creating k Thread like MyThread myThread[] = new MyThread [k];
Now one way to start a thread is to make a for loop and do .start()
on each
thread instance. But I guess that won't start it simultaneously. Is there any way
to do that? Or a better alternative to using a for loop to start all the threads?
Asked
Active
Viewed 24 times
0

nanu
- 33
- 1
- 6
-
3Does this answer your question? [How to start two threads at "exactly" the same time](https://stackoverflow.com/questions/3376586/how-to-start-two-threads-at-exactly-the-same-time) – Lalit Verma Jul 18 '21 at 06:09
-
Why exactly do you want to start the threads 'simultaneously'? – tgdavies Jul 18 '21 at 06:32
-
Literally you can't get them to start simultaneously. Indeed, if there are more threads than you have cores (or hyperthreads) then they can't run simultaneously. If your app design replies on being able to do that kind of thing, it is probably not implementable in any programming language whose implementation uses native threads (i.e. multiple cores). – Stephen C Jul 18 '21 at 06:50
-
So apart from using the trick used in another StackOverflow posts by using countdownlatch. Are there any disadvantages of starting them in for loop one by one? – nanu Jul 18 '21 at 10:34