Description: We need to perform four jobs , out of which three jobs are mutually exclusive and should be performed simultaneously(may be getting data from the database using select query) and the outcome of three jobs will be served as the input for the fourth job.Hence, the fourth job could not be started untill the first three jobs are completed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
package com.example; import java.util.ArrayList; import java.util.List; public class InnerClassTest { private static List threads = new ArrayList(); public class InnerA extends Thread { public void run() { System.out.println("InnerA");// First Job } } public class InnerB extends Thread { public void run() { System.out.println("InnerB");// Second Job } } public class InnerC extends Thread { public void run() { System.out.println("InnerC");// Third Job } } public static void main(String args[]) { InnerClassTest ict = new InnerClassTest(); InnerClassTest.InnerA inA = ict.new InnerA(); InnerClassTest.InnerB inB = ict.new InnerB(); InnerClassTest.InnerC inC = ict.new InnerC(); inA.start(); threads.add(inA); inB.start(); threads.add(inB); inC.start(); threads.add(inC); // all threads are now started for (int i = 0; i < threads.size(); i++) { try { ((Thread) threads.get(i)).join(); } catch (InterruptedException e) { e.printStackTrace(); } } // now all the threads are done System.out.println("do something");// Fourth job } } |