StackAsk is a community where developers ask questions, share answers and learn from each others real-world experience, so every solved problem helps the next person who runs into it.
import java.util.ArrayList; public class ThreadTest { static int count = 0; public static void main(String[] args) throws InterruptedException { ArrayList<Thread> list = new ArrayList<>(); for (int i = 0; i < 50; i++) { Thread th = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 200000; i++) { count++; } } }); th.start(); list.add(th); } for (Thread thread : list) { try { thread.join(); } catch (InterruptedException ex) { } } System.out.println("Count: "+count); } }
public class MultiThreadCountingTest { static AtomicInteger count = new AtomicInteger(); public static void main(String[] args) throws Exception { int threadCount = 50; List list = new ArrayList(); for (int i = 0; i { for (int i1 = 0; i1 < 200000; i1++) { count.incrementAndGet(); } }); th.start(); list.add(th); } for (Thread thread : list) { thread.join(); } System.out.println("Count: "+count); } }
public class MultiThreadCountingTest { static AtomicInteger count = new AtomicInteger(); public static void main(String[] args) throws Exception { int threadCount = 50; CountDownLatch countDownLatch = new CountDownLatch(threadCount); for (int i = 0; i { for (int i1 = 0; i1 < 200000; i1++) { count.incrementAndGet(); } countDownLatch.countDown(); }); th.start(); } countDownLatch.await(); System.out.println("Count: "+count); } }
public class MultiThreadCountingTest { static AtomicInteger count = new AtomicInteger(); public static void main(String[] args) throws Exception { int threadCount = 50; AtomicInteger doneCount = new AtomicInteger(); for (int i = 0; i { for (int i1 = 0; i1 < 200000; i1++) { count.incrementAndGet(); } doneCount.incrementAndGet(); }); th.start(); } while (doneCount.get() != threadCount) { Thread.sleep(1); } System.out.println("Count: "+count); } }
2.2K Point(s)
1.2K Point(s)
313 Point(s)
178 Point(s)
138 Point(s)
Your account is signed in on the device(s) below. Sign out of at least one of them to continue signing in on this device.
Your account is signed in on the device(s) below. Sign out directly on one of those devices, or wait until the sign-in again time shown below.
If you cant access those devices, please contact the administrator to sign them out for you.
Input your email to receive reset password link, or if you remember your password, you can click