threadsJoinWithConcurrency();
textView.setText(threadNames.toString());
===========================================
public void threadsJoinWithConcurrency() {
List<Thread> threadList = new ArrayList<Thread>();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 1000000; i++) {
i = i + 0;
}
threadNames.add("Thread-1");
}
});
threadList.add(t1);
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 1000000; i++) {
i = i + 0;
}
threadNames.add("Thread-2");
}
});
threadList.add(t2);
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 1000000; i++) {
i = i + 0;
}
threadNames.add("Thread-3");
}
});
threadList.add(t3);
for (Iterator<Thread> iterator = threadList.iterator(); iterator.hasNext();) {
Thread thread = iterator.next();
thread.start();
}
for (Iterator<Thread> iterator = threadList.iterator(); iterator.hasNext();) {
Thread thread = (Thread) iterator.next();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
threadsJoinWithSequentially();
textView.setText(threadNames.toString());
===================================
public void threadsJoinWithSequentially() {
List<Thread> threadList = new ArrayList<Thread>();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 1000000; i++) {
i = i + 0;
}
threadNames.add("Thread-1");
}
});
threadList.add(t1);
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 1000000; i++) {
i = i + 0;
}
threadNames.add("Thread-2");
}
});
threadList.add(t2);
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 1000000; i++) {
i = i + 0;
}
threadNames.add("Thread-3");
}
});
threadList.add(t3);
for (Iterator<Thread> iterator = threadList.iterator(); iterator.hasNext();) {
Thread thread = iterator.next();
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Output:
[ Thread-1, Thread-2, Thread-3]
If we don't use any join at all, threadNames, when printed, may be empty, or partially filled, since the main thread will just move on when it gets the chance. The main thread will still wait, at the very last step, for all threads to complete, before exiting the JVM. The output for running 10 threads may be:
[Thread-1, Thread-2]
...
Thread.join() is used to wait for all child threads to complete. Now I will update it using CountDownLatch instead:
threadsJoinCountDownLatch();
textView.setText(threadNames.toString());
=====================================
public void threadsJoinCountDownLatch() {
List<Thread> threadList = new ArrayList<Thread>();
final CountDownLatch latch = new CountDownLatch(3);
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 1000000; i++) {
i = i + 0;
}
threadNames.add("Thread-1");
latch.countDown();
}
});
threadList.add(t1);
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 1000000; i++) {
i = i + 0;
}
threadNames.add("Thread-2");
latch.countDown();
}
});
threadList.add(t2);
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 1000000; i++) {
i = i + 0;
}
threadNames.add("Thread-3");
latch.countDown();
}
});
threadList.add(t3);
for (Iterator<Thread> iterator = threadList.iterator(); iterator.hasNext();) {
Thread thread = iterator.next();
thread.start();
}
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
A CountDownLatch is created in main thread, passed to each child thread's constructor. Each child thread will count down by 1. After starting all child threads, the main thread then wait for the latch count to reach 0. To run it:
[ Thread-2, Thread-0, Thread-3]