Java ways 01
Java Ways 01
线程
并行,同一时刻
线程
并行,同一时刻To start the threads at
exactly
the same time (at least as good as possible), you can use a CyclicBarrier :Codes are as follows:
This doesn't have to be a
CyclicBarrier
, you could also use aCountDownLatch
or even a lock.
This still can't make sure that they are started exactly at the same time on standard JVMs, but you can get pretty close. Getting pretty close is still useful when you do for example performance tests. E.g., if you are trying to measure throughput of a data structure with different number of threads hitting it, you want to use this kind of construct to get the most accurate result possible.
On other platforms, starting threads exactly can be a very valid requirement btw.
From
StackOverFlow
Last updated