public final void join( ) throws InterruptedException public final void join(long millis) throws InterruptedException
join方法使当前线程暂停执行,直到调用join方法的线程执行结束后再继续执行当前线程。
public class JoinDemo { public static void main(String[] args) { Thread t = new Thread(new Runner(),"timer"); // timer thread t.start(); // Do stuff in parallel with the other thread for a while for(int j=0;j<100;j++) { System.out.println("##from main before join "+j); } // Wait here for the timer thread to finish try { t.join();//当前线程暂停执行直到t的线程终止为止 //t.join(10); } catch (InterruptedException e) { // t came back early and is completed } // Now continue in this thread for(int k=0;k<100;k++) { System.out.println("**from main after join "+k); } } } // end of class
class Runner implements Runnable { public void run() { for(int i=0;i<200;i++) { System.out.println("@@@from runner"+i); } } }
实际输出可能为: ==============================================================
##from main before join 0 ##from main before join 1 ##from main before join 2 ##from main before join 3 ##from main before join 4 ##from main before join 5 ##from main before join 6 ##from main before join 7 ##from main before join 8 ##from main before join 9 ##from main before join 10 ##from main before join 11 ##from main before join 12 ##from main before join 13 ##from main before join 14 @@@from runner0 @@@from runner1 @@@from runner2 @@@from runner3 ##from main before join 15 @@@from runner4 ##from main before join 16 @@@from runner5 ##from main before join 17 ##from main before join 18 ##from main before join 19 ##from main before join 20 ##from main before join 21 ##from main before join 22 ##from main before join 23 ##from main before join 24 ##from main before join 25 ##from main before join 26 ##from main before join 27 ##from main before join 28 @@@from runner6 @@@from runner7 @@@from runner8 @@@from runner9 @@@from runner10 @@@from runner11 @@@from runner12 @@@from runner13 @@@from runner14 @@@from runner15 @@@from runner16 @@@from runner17 @@@from runner18 @@@from runner19 @@@from runner20 @@@from runner21 @@@from runner22 @@@from runner23 @@@from runner24 @@@from runner25 @@@from runner26 @@@from runner27 @@@from runner28 @@@from runner29 @@@from runner30 @@@from runner31 ##from main before join 29 ##from main before join 30 ##from main before join 31 ##from main before join 32 ##from main before join 33 ##from main before join 34 ##from main before join 35 ##from main before join 36 ##from main before join 37 ##from main before join 38 ##from main before join 39 ##from main before join 40 ##from main before join 41 ##from main before join 42 ##from main before join 43 ##from main before join 44 ##from main before join 45 ##from main before join 46 ##from main before join 47 ##from main before join 48 ##from main before join 49 ##from main before join 50 ##from main before join 51 @@@from runner32 @@@from runner33 @@@from runner34 @@@from runner35 @@@from runner36 @@@from runner37 @@@from runner38 @@@from runner39 @@@from runner40 @@@from runner41 @@@from runner42 @@@from runner43 @@@from runner44 @@@from runner45 @@@from runner46 @@@from runner47 @@@from runner48 ##from main before join 52 ##from main before join 53 ##from main before join 54 ##from main before join 55 ##from main before join 56 ##from main before join 57 ##from main before join 58 ##from main before join 59 ##from main before join 60 ##from main before join 61 ##from main before join 62 ##from main before join 63 ##from main before join 64 ##from main before join 65 ##from main before join 66 @@@from runner49 @@@from runner50 @@@from runner51 @@@from runner52 @@@from runner53 @@@from runner54 @@@from runner55 @@@from runner56 @@@from runner57 @@@from runner58 @@@from runner59 @@@from runner60 @@@from runner61 @@@from runner62 @@@from runner63 @@@from runner64 @@@from runner65 @@@from runner66 @@@from runner67 @@@from runner68 @@@from runner69 @@@from runner70 @@@from runner71 @@@from runner72 @@@from runner73 @@@from runner74 ##from main before join 67 ##from main before join 68 ##from main before join 69 ##from main before join 70 ##from main before join 71 ##from main before join 72 ##from main before join 73 ##from main before join 74 ##from main before join 75 ##from main before join 76 ##from main before join 77 ##from main before join 78 ##from main before join 79 ##from main before join 80 ##from main before join 81 ##from main before join 82 ##from main before join 83 ##from main before join 84 ##from main before join 85 ##from main before join 86 ##from main before join 87 ##from main before join 88 ##from main before join 89 @@@from runner75 @@@from runner76 @@@from runner77 @@@from runner78 @@@from runner79 @@@from runner80 @@@from runner81 @@@from runner82 @@@from runner83 @@@from runner84 @@@from runner85 @@@from runner86 @@@from runner87 @@@from runner88 @@@from runner89 @@@from runner90 @@@from runner91 ##from main before join 90 ##from main before join 91 ##from main before join 92 ##from main before join 93 ##from main before join 94 ##from main before join 95 ##from main before join 96 ##from main before join 97 ##from main before join 98 ##from main before join 99 @@@from runner92 @@@from runner93 @@@from runner94 @@@from runner95 @@@from runner96 @@@from runner97 @@@from runner98 @@@from runner99 @@@from runner100 @@@from runner101 @@@from runner102 @@@from runner103 @@@from runner104 @@@from runner105 @@@from runner106 @@@from runner107 @@@from runner108 @@@from runner109 @@@from runner110 @@@from runner111 @@@from runner112 @@@from runner113 @@@from runner114 @@@from runner115 @@@from runner116 @@@from runner117 @@@from runner118 @@@from runner119 @@@from runner120 @@@from runner121 @@@from runner122 @@@from runner123 @@@from runner124 @@@from runner125 @@@from runner126 @@@from runner127 @@@from runner128 @@@from runner129 @@@from runner130 @@@from runner131 @@@from runner132 @@@from runner133 @@@from runner134 @@@from runner135 @@@from runner136 @@@from runner137 @@@from runner138 @@@from runner139 @@@from runner140 @@@from runner141 @@@from runner142 @@@from runner143 @@@from runner144 @@@from runner145 @@@from runner146 @@@from runner147 @@@from runner148 @@@from runner149 @@@from runner150 @@@from runner151 @@@from runner152 @@@from runner153 @@@from runner154 @@@from runner155 @@@from runner156 @@@from runner157 @@@from runner158 @@@from runner159 @@@from runner160 @@@from runner161 @@@from runner162 @@@from runner163 @@@from runner164 @@@from runner165 @@@from runner166 @@@from runner167 @@@from runner168 @@@from runner169 @@@from runner170 @@@from runner171 @@@from runner172 @@@from runner173 @@@from runner174 @@@from runner175 @@@from runner176 @@@from runner177 @@@from runner178 @@@from runner179 @@@from runner180 @@@from runner181 @@@from runner182 @@@from runner183 @@@from runner184 @@@from runner185 @@@from runner186 @@@from runner187 @@@from runner188 @@@from runner189 @@@from runner190 @@@from runner191 @@@from runner192 @@@from runner193 @@@from runner194 @@@from runner195 @@@from runner196 @@@from runner197 @@@from runner198 @@@from runner199 **from main after join 0 **from main after join 1 **from main after join 2 **from main after join 3 **from main after join 4 **from main after join 5 **from main after join 6 **from main after join 7 **from main after join 8 **from main after join 9 **from main after join 10 **from main after join 11 **from main after join 12 **from main after join 13 **from main after join 14 **from main after join 15 **from main after join 16 **from main after join 17 **from main after join 18 **from main after join 19 **from main after join 20 **from main after join 21 **from main after join 22 **from main after join 23 **from main after join 24 **from main after join 25 **from main after join 26 **from main after join 27 **from main after join 28 **from main after join 29 **from main after join 30 **from main after join 31 **from main after join 32 **from main after join 33 **from main after join 34 **from main after join 35 **from main after join 36 **from main after join 37 **from main after join 38 **from main after join 39 **from main after join 40 **from main after join 41 **from main after join 42 **from main after join 43 **from main after join 44 **from main after join 45 **from main after join 46 **from main after join 47 **from main after join 48 **from main after join 49 **from main after join 50 **from main after join 51 **from main after join 52 **from main after join 53 **from main after join 54 **from main after join 55 **from main after join 56 **from main after join 57 **from main after join 58 **from main after join 59 **from main after join 60 **from main after join 61 **from main after join 62 **from main after join 63 **from main after join 64 **from main after join 65 **from main after join 66 **from main after join 67 **from main after join 68 **from main after join 69 **from main after join 70 **from main after join 71 **from main after join 72 **from main after join 73 **from main after join 74 **from main after join 75 **from main after join 76 **from main after join 77 **from main after join 78 **from main after join 79 **from main after join 80 **from main after join 81 **from main after join 82 **from main after join 83 **from main after join 84 **from main after join 85 **from main after join 86 **from main after join 87 **from main after join 88 **from main after join 89 **from main after join 90 **from main after join 91 **from main after join 92 **from main after join 93 **from main after join 94 **from main after join 95 **from main after join 96 **from main after join 97 **from main after join 98 **from main after join 99
join方法之前的输出,和t线程内部的输出可能是交替的,而 join方法之后的输出,一定在t线程结束之后。
|