Because sleep, wait, join, and so on are blocked, you can use interrupted exception to wake unexpectedly.
First, the role
The primary role of the Join method in the thread class is synchronization, which enables parallel execution between threads to become serial execution.
Package Cn.com.upcard;
public class ThreadTest {
public static void Main (string[] args) throws Interruptedexception {
threadjointest T1 = new Threadjointest ("Xiaoming");
Threadjointest t2 = new Threadjointest ("small East");
T1.start ();
/**
* Join means that the execution of the current thread is discarded and the corresponding thread is returned, for example, as follows:
* The program calls the join method of the T1 thread in the main thread, and the main thread discards the CPU control
*, and returns the T1 thread to continue execution until the thread T1 executes, so the result is T1 after the thread finishes executing
* Before the main thread executes, the equivalent of synchronizing the T1 thread in the main thread, T1 executes, and the main threads have an opportunity to execute.
*/
T1.join ();
T2.start ();
}
}
Class Threadjointest extends Thread {
Public threadjointest (String name) {
Super (name);
}
@Override
public void Run () {
for (int i = 0; i <; i++) {
System.out.println (This.getname () + ":" + i);
}
}
}
Operation Result:
Xiaoming: 0 Xiao Ming: 1 Xiao Ming: 2 Xiao Ming: 3 Xiao Ming: 4 Xiao Ming: 5 xiaoming: 6 Xiao Ming: 7 Xiao Ming: 8 Xiao Ming: 9 Xiao Ming: 10 Xiao Ming: 11 Xiao Ming: 12 Xiao Ming: 13 Xiao Ming: 14 Xiao Ming: 15 Xiao Ming: 16 Xiao Ming: 17 Xiao Ming: 18 Xiao Ming: 19 Xiao Ming: 20 Xiao Ming: 21 Xiao Ming: 22 Xiao Ming : 23 Xiao Ming: 24 Xiao Ming: 25 Xiao Ming: 26 Xiao Ming: 27 Xiao Ming: 28 Xiao Ming: 29 Xiao Ming: 30 Xiao Ming: 31 Xiao Ming: 32 Xiao Ming: 33 Xiao Ming: 34 Xiao Ming: 35 Xiao Ming: 36 Xiao Ming: 37 Xiao Ming: 38 Xiao Ming: 39 Xiao Ming: 40 Xiao Ming: 41 Xiao Ming: 42 Xiao Ming: 43 Xiao Ming: 44 Xiao Ming : 45 Xiao Ming: 46 Xiao Ming: 47 Xiao Ming: 48 Xiao Ming: 49 Xiao Ming: 50 xiao Ming: 51 Xiao Ming: 52 Xiao Ming: 53 Xiao Ming: 54 Xiao Ming: 55 Xiao Ming: 56 Xiao Ming: 57 Xiao Ming: 58 Xiao Ming: 59 Xiao Ming: 60 Xiao Ming: 61 Xiao Ming: 62 Xiao Ming: 63 Xiao Ming: 64 Xiao Ming: 65 Xiao Ming: 66 Xiao Ming : 67 Xiao Ming: 68 Xiao Ming: 69 Xiao Ming: 70 Xiao Ming: 71 Xiao Ming: 72 Xiao Ming: 73 Xiao Ming: 74 Xiao Ming: 75 xiao Ming: 76 Xiao Ming: 77 Xiao Ming: 78 Xiao Ming: 79 Xiao Ming: 80 Xiao Ming: 81 Xiao Ming: 82 Xiao Ming: 83 Xiao Ming: 84 Xiao Ming: 85 Xiao Ming: 86 Xiao Ming: 87 Xiao Ming: 88 Xiao Ming : 89 Xiao Ming: 90 Xiao Ming: 91 Xiao Ming: 92 Xiao Ming: 93 Xiao Ming: 94 Xiao Ming: 95 Xiao Ming: 96 Xiao Ming: 97 Xiao Ming: 98 Xiao Ming: 99 small East: 0 Small East: 1 small East: 2 small East: 3 small East: 4 small East: 5 East: 6 small East: 7 small East: 8 small East: 9 small East: 10 small East: 11 small East: 12 small East : 13 Little East: 14 Little East: 15 Little East: 16 small East: 17 small East: 18 small East: 19 small East: 20 small East: 21 small East: 22 small East: 23 small East: 24 small East: 25 small East: 26 small East: 27 small East: 28 small East: 29 small East £ º 30 small east: 31 small East: 32 small East: 33 small East: 34 small East : 35 Little East: 36 Little East: 37 Little East: 38 small East: 39 small East: 40 small East: 41 small East: 42 small East: 43 small East: 44 small East: 45 small East: 46 small East: 47 small East: 48 small East: 49 small East: 50 small East: 51 small East £ º 52 small east: 53 small East: 54 small East: 55 small East: 56 small East : 57 Little East: 58 Little East: 59 Little East: 60 small East: 61 small East: 62 small East: 63 small East: 64 small East: 65 small East: 66 small East: 67 small East: 68 small East: 69 small East: 70 small East: 71 small East: 72 small East: 73 small East £ º 74 small east: 75 small East: 76 small East: 77 small East: 78 small East : 79 Little East: 80 small East: 81 small East: 82 small East: 83 Small East: 84 small East: 85 Small East: 86 small East: 87 small East: 88 Small East: 89 small East: 90 Small East: 91 small East: 92 small East: 93 small East, 94 small east: 95 Small East: 96 small East: 97 small East: 98 Small East: 99
You can see that the first thread is printed before the second thread is printed. Description: When a B-thread's Join () method is called in a thread, it means that the a thread can continue execution only when the B thread finishes executing, and note that the join method called here is not a parameter, and the join method can actually pass an argument to it.
Package Cn.com.upcard;public class ThreadTest {public static void main (string[] args) throws Interruptedexception {Thread jointest T1 = new Threadjointest ("Xiaoming"); Threadjointest t2 = new Threadjointest ("small East"); T1.start ();/** * The Join method can pass parameters, join (1) indicates that the thread will wait for the T1 thread for 1 milliseconds, 1 milliseconds past, The execution order between the main thread and the T1 thread is changed from serial execution to normal parallel execution */t1.join (2); T2.start ();}} Class Threadjointest extends Thread {public threadjointest (String name) {super (name);} @Overridepublic void Run () {for (int i = 0; i < i++) {System.out.println (This.getname () + ":" + i);}}
Execution Result:
Xiaoming: 0 Xiao Ming: 1 Xiao Ming: 2 Xiao Ming: 3 Xiao Ming: 4 Xiao Ming: 5 xiaoming: 6 Xiao Ming: 7 Xiao Ming: 8 Xiao Ming: 9 Xiao Ming: 10 Xiao Ming: 11 Xiao Ming: 12 Xiao Ming: 13 Xiao Ming: 14 Xiao Ming: 15 Xiao Ming: 16 Xiao Ming: 17 Xiao Ming: 18 Xiao Ming: 19 Xiao Ming: 20 Xiao Ming: 21 Xiao Ming: 22 Xiao Ming : 23 Xiao Ming: 24 Xiao Ming: 25 Xiao Ming: 26 Xiao Ming: 27 Xiao Ming: 28 Xiao Ming: 29 Xiao Ming: 30 Xiao Ming: 31 Xiao Ming: 32 Xiao Ming: 33 Xiao Ming: 34 Xiao Ming: 35 Xiao Ming: 36 Xiao Ming: 37 Xiao Ming: 38 Xiao Ming: 39 Xiao Ming: 40 Xiao Ming: 41 Xiao Ming: 42 Xiao Ming: 43 Xiao Ming: 44 Xiao Ming : 45 Xiao Ming: 46 Xiao Ming: 47 Xiao Ming: 48 Xiao Ming: 49 Xiao Ming: 50 xiao Ming: 51 Xiao Ming: 52 Xiao Ming: 53 Xiao Ming: 54 Xiao Ming: 55 Xiao Ming: 56 Xiao Ming: 57 Xiao Ming: 58 Xiao Ming: 59 Xiao Ming: 60 Xiao Ming: 61 Xiao Ming: 62 Xiao Ming: 63 Xiao Ming: 64 Xiao Ming: 65 Xiao Ming: 66 Xiao Ming : 67 Xiao Ming: 68 Xiao Ming: 69 Xiao Ming: 70 Xiao Ming: 71 Xiao Ming: 72 Xiao Ming: 73 Xiao Ming: 74 Xiao Ming: 75 xiao Ming: 76 Xiao Ming: 77 Xiao Ming: 78 Xiao Ming: 79 Xiao Ming: 80 Xiao Ming: 81 Xiao Ming: 82 Xiao Ming: 83 Xiao Ming: 84 Xiao Ming: 85 Xiao Ming: 86 Xiao Ming: 87 Xiao Ming: 88 Xiao Ming : 89 Xiao Ming: 90 Xiao Ming: 91 Xiao Ming: 92 Xiao Ming: 93 Xiao Ming: 94 Xiao Ming: 95 Xiao Ming: 96 Xiao Ming: 97 Xiao Ming: 98 Xiao Ming: 99 Xiao Ming: 100 Xiao Ming: 101 Xiao Ming: 102 Xiao Ming: 103 Xiao Ming: 104 Xiao Ming: 105 Xiao Ming: 106 Xiao Ming: 107 Xiao Ming: 108 Xiao Ming : 109 Xiao Ming: 110 Small ming: 111 Xiao Ming: 112 Xiao Ming: 113 Xiao Ming: 114 Xiao Ming: 115 Xiao Ming: 116 Xiao Ming: 117 Xiao Ming: 118 Xiao Ming: 119 Xiao Ming: 120 xiao Ming: 121 Xiao Ming: 122 Xiao Ming: 123 Xiao Ming: 124 Xiao Ming: 125 Xiao Ming: 126 Xiao Ming : 127 Xiao Ming: 128 Small Ming: 129 xiao Ming: 130 Xiao Ming: 131 Xiao Ming: 132 Xiao Ming: 133 Xiao Ming: 134 Xiao Ming: 135 Xiao Ming: 136 Xiao Ming: 137 Xiao Ming: 138 Xiao Ming: 139 Xiao Ming: 140 Xiao Ming: 141 Xiao Ming: 142 Xiao Ming: 143 Xiao Ming: 144 Xiao Ming : 145 Xiao Ming: 146 small ming: 147 Xiao Ming: 148 Xiao Ming: 149 Xiao Ming: 150 xiao Ming: 151 Xiao Ming: 152 Xiao Ming: 153 Xiao Ming: 154 Xiao Ming: 155 Xiao Ming: 156 Xiao Ming: 157 Xiao Ming: 158 xiao Ming: 159 Xiao Ming: 160 Xiao Ming: 161 Xiao Ming: 162 Xiao Ming : 163 Xiao Ming: 164 small Ming: 165 xiao Ming: 166 Xiao Ming: 167 Xiao Ming: 168 Xiao Ming: 169 Xiao Ming: 170 Xiao Ming: 171 Xiao Ming: 172 Xiao Ming: 173 Xiao Ming: 174 xiao Ming: 175 Xiao Ming: 176 Xiao Ming: 177 xiao Ming: 178 Xiao Ming: 179 Xiao Ming: 180 Xiao Ming : 181 Xiao Ming: 182 xiao Ming: 183 Xiao Ming: 184Xiaoming: 185 Xiao Ming: 186 small Ming: 187 Xiao Ming: 188 Xiao Ming: 189 xiao Ming: 190 Xiao Ming: 191 Xiao Ming: 192 xiao Ming: 193 Xiao Ming: 194 Xiao Ming: 195 Xiao Ming: 196 Xiao Ming: 197 Xiao Ming: 198 Xiao Ming: 199 Xiao Ming: 200 Xiao Ming: 201 Xiao Ming: 202 Xiao Ming : 203 Xiao Ming: 204 Xiao Ming: 205 Xiao Ming: 206 Xiao Ming: 207 Xiao Ming: 208 Xiao Ming: 209Xiaoming: 210 Xiao Ming: 211 Xiao Ming: 212 Xiao Ming: 213 Xiao Ming: 214 small East: 0 Small East: 1 small East: 2 small East: 3 small East: 4 small East: 5 small East: 6 small East: 7 small East: 8 small East: 9 small East: 10 small East: 11 small East £ º 12 Jr. East: 13 Small East: 14 small East: 15 small East: 16 small East : 17 Little East: 18 Little East: 19 Little East: 20 small East: 21 small East: 22 small East: 23 small East: 24 small East: 25 small East: 26 small East: 27 small East: 28 small East: 29 small East: 30 small East: 31 small East: 32 small East: 33 small East £ º 34 small east: 35 small East: 36 small East: 37 small East: 38 small East : 39 Little East: 40 Little East: 41 Little East: 42 small East: 43 small East: 44 small East: 45 small East: 46 small East: 47 small East: 48 small East: 49 small East: 50 small East: 51 small East: 52 Small East: 53 small East: 54 small East: 55 small East £ º 56 small east: 57 Small East: 58 small East: 59 small East: 60 small East : 61 Little East: 62 Little East: 63 Little East: 64 small East: 65 small East: 66 small East: 67 small East: 68 small East: 69 small East: 70 small East: 71 small East: 72 small East: 73 small East: 74 small East: 75 small East: 76 small East: 77 small East £ º 78 small east: 79 small East: 80 small East: 81 small East: 82 small East : 83 Little East: 84 Little East: 85 Small East: 86 small East: 87 small East: 88 Small East: 89 small East: 90 Small East: 91 small East: 92 small East: 93 Small East: 94 Small East: 95 Small East: 96 small East: 97 small East: 98 small East £ º 99 small east: 100 small East: 101 small East: 102 Small East: 103 small East : 104 Small East: 105 small East: 106 Small East: 107 small East: 108 small East: 109 small east: 110 small East: 111 small East: 112 small East: 113 Small East: 114 small East: 115 small East: 116 small East: 117 small East: 118 small East: 119 Small East: 120 small east: 121 small East : 122 small East: 123 small East: 124 small East: 125 small East: 126 small East: 127 small East: 128 small East: 129 small East: 130 small East: 131 small East: 132 small East: 133 small East: 134 small East: 135 small East: 136 small East: 137 small East: 138 small east: 139 small East : 140 Small East: 141 small East: 142 small East: 143 small East: 144 small East: 145 small East: 146 small East: 147 small East: 148 small east: 149 Small East: 150 small East: 151 small East: 152 Small East: 153 small East: 154 small East: 155 small East: 156 small East: 157 small East : 158 small East: 159 small East: 160 small East: 161 small East: 162 small East: 163 small East: 164 small East: 165 small East: 166 small East: 167 small East: 168 small East: 169 small East: 170 small East: 171 small East: 172 small East: 173 small East: 174 small East: 175 small East : 176 Little East: 177 little East: 178 Little East: 179Small East: 180 small East: 181 small East: 182 small East: 183 small East: 184 small East: 185 small East: 186 small East: 187 small East: 188 small East: 189 small East: 190 small East: 191 small East: 192 small East: 193 small East: 194 small East: 195 small East: 196 small East: 197 small East : 198 small East: 199 small East: 200 small East: 201 Small East: 202 small East: 203 small East: 204 small East: 205 small East: 206 small East: 207 small East: 208 small East: 209 Small East: 210 small East: 211 small East: 212 small East: 213 small East: 214 small East: 215 small East : 216 Small East: 217 small East: 218 Small East: 219 small East: 220 small East: 221 small East: 222 small East: 223 small East: 224 small East: 225 small East: 226 small East: 227 small East: 228 small East: 229 small East: 230 small East: 231 small East: 232 small East: 233 small East : 234 small East: 235 small East: 236 small East: 237 small East: 238 small East: 239 small East: 240 small East: 241 small East: 242 small East: 243 small East: 244 small East: 245 small East: 246 small East: 247 small East: 248 small East: 249 small East: 250 small East: 251 small East : 252 small East: 253 small East: 254 small East: 255 small East: 256 small East: 257 small East: 258 small East: 259 small East: 260 small East: 261 small East: 262 small East: 263 small East: 264 small East: 265 small East: 266 small East: 267 small East: 268 small East: 269 small East : 270 small East: 271 small East: 272 Small East: 273 small East: 274 small East: 275 small East: 276 small East: 277 small East: 278 small East: 279 small East: 280 small East: 281 small East: 282 small East: 283 small East: 284 small East: 285 small East: 286 small East: 287 small East : 288 Xiao Ming: 215 small Ming: 216 Xiao Ming: 217 Xiao Ming: 218 Xiao Ming: 219 Xiao Ming: 220 Xiao Ming: 221 Xiao Ming: 222 Xiao Ming: 223 Xiao Ming: 224 Xiao Ming: 225 Xiao Ming: 226Xiaoming: 227 Xiao Ming: 228 small ming: 229 Xiao Ming: 230 Xiao Ming: 231 Xiao Ming: 232 Xiao Ming: 233 Xiao Ming: 234 Xiao Ming: 235 Xiao Ming: 236 Xiao Ming: 237 Xiao Ming: 238 Xiao Ming: 239 Xiao Ming: 240 Xiao Ming: 241 Xiao Ming: 242 Xiao Ming: 243 Xiao Ming: 244 Xiao Ming : 245 Xiao Ming: 246 small ming: 247 Xiao Ming: 248 Xiao Ming: 249 Xiao Ming: 250 xiao Ming: 251 Xiao Ming: 252 Xiao Ming: 253 xiao Ming: 254 Xiao Ming: 255 Xiao Ming: 256 Xiao Ming: 257 Xiao Ming: 258 Xiao Ming: 259 Xiao Ming: 260 Xiao Ming: 261 xiao Ming: 262 Xiao Ming : 263 Xiao Ming: 264 small Ming: 265 xiao Ming: 266 xiao Ming: 267 Xiao Ming: 268 Xiao Ming: 269 xiao Ming: 270 Xiao Ming: 271 Xiao Ming: 272 Xiao Ming: 273 Xiao Ming: 274 Xiao Ming: 275 Xiao Ming: 276 xiao Ming: 277 Xiao Ming: 278 xiao Ming: 279 Xiao Ming: 280 Xiao Ming : 281 Xiao Ming: 282 small ming: 283 Xiao Ming: 284 Xiao Ming: 285 xiao Ming: 286 Xiao Ming: 287 Xiao Ming: 288 xiao Ming: 289 Xiao Ming: 290 Xiao Ming: 291 Xiao Ming: 292 Xiao Ming: 293 Xiao Ming: 294 Xiao Ming: 295 Xiao Ming: 296 xiao Ming: 297 Xiao Ming: 298 Xiao Ming : 299 Xiao Ming: 300 small ming: 301 Xiao Ming: 302 Xiao Ming: 303 Xiao Ming: 304 Xiao Ming: 305 Xiao Ming: 306 Xiao Ming: 307 Xiao Ming: 308 Xiao Ming: 309 Xiao Ming: 310 Xiao Ming: 311 Xiao Ming: 312 Xiao Ming: 313 Xiao Ming: 314 Xiao Ming: 315 Xiao Ming: 316 Xiao Ming : 317 Xiao Ming: 318 small Ming: 319 Xiao Ming: 320 Xiao Ming: 321 Xiao Ming: 322 Xiao Ming: 323 xiao Ming: 324 Xiao Ming: 325 xiao Ming: 326 Xiao Ming: 327 Xiao Ming: 328 Xiao Ming: 329 Xiao Ming: 330 Xiao Ming: 331 Xiao Ming: 332 Xiao Ming: 333 Xiao Ming: 334 Xiao Ming : 335 Xiao Ming: 336 small ming: 337 Xiao Ming: 338 Xiao Ming: 339 Xiao Ming: 340 Xiao Ming: 341 Xiao Ming: 342 Xiao Ming: 343 Xiao Ming: 344 xiao Ming: 345 xiao Ming: 346 xiao Ming: 347 Xiao Ming: 348 Xiao Ming: 349 xiao Ming: 350 Xiao Ming: 351 Xiao Ming: 352 Xiao Ming : 353 Xiao Ming: 354 small ming: 355 Xiao Ming: 356 Xiao Ming: 357 Xiao Ming: 358 xiao Ming: 359 Xiao Ming: 360 Xiao Ming: 361 Xiao Ming: 362 Xiao Ming: 363 Xiao Ming: 364 xiao Ming: 365 Xiao Ming: 366 Xiao Ming: 367 Xiao Ming: 368 xiao Ming: 369 Xiao Ming: 370 Xiao Ming : 371 Xiao Ming: 372 small ming: 373 Xiao Ming: 374 Xiao Ming: 375 Xiao Ming: 376 Xiao Ming: 377 Xiao Ming: 378 Xiao Ming: 379 Xiao Ming: 380 Xiao Ming: 381 Xiao Ming: 382 Xiao Ming: 383 Xiao Ming: 384 xiao Ming: 385 Xiao Ming: 386 xiao Ming: 387 Xiao Ming: 388 Xiao Ming : 389 Xiao Ming: 390 Xiao Ming: 391 Xiao Ming: 392 Xiao Ming: 393 Xiao Ming: 394 Xiao Ming: 395 Xiao Ming: 396 Xiao Ming: 397 Xiao Ming: 398 Xiao Ming: 399 Xiao Ming: 400 xiao Ming: 401 Xiao Ming: 402 Xiao Ming: 403 xiao Ming: 404 Xiao Ming: 405 Xiao Ming: 406 Xiao Ming: 407 Xiao Ming: 408 Xiao Ming: 409 xiao Ming: 410 Xiao Ming : 411 Xiao Ming: 412 Small ming: 413 xiao Ming: 414 Xiao Ming: 415 Xiao Ming: 416 Xiao Ming: 417 Xiao Ming: 418 Xiao Ming: 419 Xiao Ming: 420 Xiao Ming: 421 Xiao Ming: 422 Xiao Ming: 423 Xiao Ming: 424 Xiao Ming: 425 Xiao Ming: 426 Xiao Ming: 427 Xiao Ming: 428 Xiao Ming : 429 Xiao Ming: 430 Small Ming: 431 Xiao Ming: 432 Xiao Ming: 433 Xiao Ming: 434 Xiao Ming: 435 Xiao Ming: 436 Xiao Ming: 437 Xiao Ming: 438 xiao Ming: 439 Xiao Ming: 440 Xiao Ming: 441 Xiao Ming: 442 xiao Ming: 443 xiao Ming: 444 Xiao Ming: 445 xiao Ming: 446 Xiao Ming : 447 Xiao Ming: 448 small ming: 449 Xiao Ming: 450 Xiao Ming: 451 Xiao Ming: 452 Xiao Ming: 453 Xiao Ming: 454 Xiao Ming: 455 xiao Ming: 456 xiao Ming: 457 xiao Ming: 458 Xiao Ming: 459 Xiao Ming: 460 Xiao Ming: 461 Xiao Ming: 462 Xiao Ming: 463 Xiao Ming: 464 Xiao Ming : 465 Xiao Ming: 466 small ming: 467 Xiao Ming: 468 Xiao Ming: 469 Xiao Ming: 470 Xiao Ming: 471 Xiao Ming: 472 xiao Ming: 473 Xiao Ming: 474 Xiao Ming: 475 Xiao Ming: 476 Xiao Ming: 477 Xiao Ming: 478 Xiao Ming: 479 Xiao Ming: 480 Xiao Ming: 481 xiao Ming: 482 Xiao Ming : 483 Xiao Ming: 484 small ming: 485 xiao Ming: 486 Xiao Ming: 487 Xiao Ming: 488 Xiao Ming: 489 Xiao Ming: 490 Xiao Ming: 491 Xiao Ming: 492 Xiao Ming: 493 Xiao Ming: 494 Xiao Ming: 495 Xiao Ming: 496 Xiao Ming: 497 Xiao Ming: 498 Xiao Ming: 499 xiao Dong: 289 Xiao Dong : 290 Small East: 291 small East: 292 small East: 293 small East: 294 small East: 295 small East: 296 small East: 297 small East: 298 small East: 299 small East: 300 small East: 301 Small East: 302 Small East: 303 small East: 304 Small East: 305 small East: 306 small East: 307 small East : 308 small East: 309 Small East: 310 Small East: 311 small East: 312 small East: 313 Small East: 314 small East: 315 small East: 316 small East: 317 small East: 318 small East: 319 Small East: 320 small East: 321 small East: 322 Small East: 323 small east: 324 small East: 325 small East : 326 small East: 327 small East: 328 small East: 329 small East: 330 Small East: 331 small East: 332 small East: 333 small East: 334 small East: 335 small East: 336 small East: 337 small East: 338 small East: 339 small East: 340 small East: 341 small East: 342 small East: 343 small East : 344 little East: 345 Little East: 346 East: 347 Little East: 348 Little East: 349 small East: 350 small East: 351 small East: 352 small East: 353 small East: 354 small East: 355 small East: 356 small East: 357 small East: 358 small East: 359 small East: 360 small East: 361 small East: 362 small East: 363 small East: 364 small east: 365 small East: 366 small East : 367 small East: 368 small East: 369 small East: 370 small East: 371 small East: 372 small East: 373 small East: 374 small East: 375 small East: 376 Small East: 377 small East: 378 small East: 379 Small East: 380 small East: 381 small East: 382 small East: 383 small East: 384 small East : 385 Small East: 386 small East: 387 small East: 388 small East: 389 small East: 390 small East: 391 small East: 392 small East: 393 small East: 394 small East: 395 small East: 396 small East: 397 small East: 398 small East: 399 Small East: 400 small East: 401 small East: 402 small East : 403 small East: 404 small East: 405 small East: 406 small East: 407 Small East: 408 small East: 409 small East: 410 small East: 411 small East: 412 Small East: 413 small East: 414 Small East: 415 small East: 416 small East: 417 small East: 418 Small East: 419 small East: 420 small East : 421 Small East: 422 small East: 423 small East: 424 Small East: 425 small East: 426 small East: 427 small East: 428 small East: 429 Small East: 430 Small East: 431 small East: 432 small East: 433 small East: 434 Small East: 435 small East: 436 small East: 437 small East: 438 small East : 439 small East: 440 small East: 441 small East: 442 small East: 443 small East: 444 small East: 445 small East: 446 small East: 447 small East: 448 small East: 449 small East: 450 small east: 451 small East: 452 small East: 453 small East: 454 small East: 455 small East: 456 small East : 457 small East: 458 small East: 459 small East: 460 Small East: 461 small East: 462 small East: 463 small East: 464 small East: 465 small East: 466 small East: 467 small East: 468 small East: 469 small East: 470 small East: 471 small East: 472 small East: 473 small East: 474 small East : 475 small East: 476 small East: 477 small East: 478 small East: 479 small East: 480 small East: 481 small East: 482 small East: 483 small East: 484 small East: 485 small East: 486 small East: 487 small East: 488 small East: 489 small East: 490 small East: 491 small East: 492 small East : 493 little East: 494 little East: 495 East: 496 Little East: 497 Little East: 498 Little East: 499
The first 2 milliseconds are printed with the number one thread, followed by alternating printing.
Therefore, if a parameter is passed in the Join method, it means that if the a thread is dropped with a join of the B thread (10), the A thread waits for the B thread to execute for 10 milliseconds, and after 10 milliseconds, the A and B threads execute in parallel. It is important to note that the JDK specifies that join (0) does not mean that a thread waits for the B thread for 0 seconds, but that a thread waits for the b thread for an unlimited time until the B thread executes, that is, join (0) is equivalent to join ().
Ii. effects of Join and start call order on the result
Package Cn.com.upcard;public class ThreadTest {public static void main (string[] args) throws Interruptedexception {Thread jointest T1 = new Threadjointest ("Xiaoming"); Threadjointest t2 = new Threadjointest ("Small East"),/** * Join method is called before start and does not play the role of Synchronization */t1.join (); T1.start (); T2.start ();}} Class Threadjointest extends Thread {public threadjointest (String name) {super (name);} @Overridepublic void Run () {for (int i = 0; i < i++) {System.out.println (This.getname () + ":" + i);}}
Xiaoming: 0 Xiao Ming: 1 Xiao Ming: 2 Xiao Ming: 3 Xiao Ming: 4 Xiao Ming: 5 xiaoming: 6 Xiao Ming: 7 Xiao Ming: 8 Xiao Ming: 9 Xiao Ming: 10 Xiao Ming: 11 Xiao Ming: 12 Xiao Ming: 13 Xiao Ming: 14 Xiao Ming: 15 Xiao Ming: 16 Xiao Ming: 17 Xiao Ming: 18 Xiao Ming: 19 Xiao Ming: 20 Xiao Ming: 21 Xiao Ming: 22 Xiao Ming : 23 Xiao Ming: 24 Xiao Ming: 25 Xiao Ming: 26 Xiao Ming: 27 Xiao Ming: 28 Xiao Ming: 29 Xiao Ming: 30 Xiao Ming: 31 Xiao Ming: 32 small East: 0 Xiao Ming: 33 small East: 1 Xiao Ming: 34 small East: 2 Xiao Ming: 35 small East: 3 Xiao Ming: 36 Small: 37 small east: 4 Xiao Ming: 38 small East: 5 Xiao Ming: 39 small East : 6 Xiao Ming: 40 small East: 7 small East: 8 small East: 9 small East: 10 small East: 11 small East: 12 small East: 13 small East: 14 small East: 15 small East: 16 small East: 17 small East: 18 small East: 19 small East: 20 small East: 21 small East £ º 22 small east: 23 small East: 24 small East: 25 small East: 26 small East : 27 Little East: 28 Little East: 29 Little East: 30 small East: 31 small East: 32 small East: 33 small East: 34 small East: 35 Xiao Ming: 41 small East: 36 small East: 37 small East: 38 small East: 39 small East: 40 small East: 41 small East: 42 small East £ º 43 Jr. East: 44 small East: 45 small East: 46 small East: 47 small East : 48 Little East: 49 Little East: 50 little East: 51 small East: 52 Small East: 53 small East: 54 small East: 55 small East: 56 small East: 57 Small East: 58 small East: 59 small East: 60 small East: 61 small East: 62 small East: 63 small East: 64 small East £ º 65 small east: 66 small East: 67 small East: 68 small East: 69 small East : 70 Little East: 71 Little East: 72 Little East: 73 small East: 74 small East: 75 small East: 76 small East: 77 small East: 78 small East: 79 small East: 80 small East: 81 small East: 82 small East: 83 Small East: 84 small East: 85 Small East: 86 small East £ º 87 small east: 88 Small East: 89 small East: 90 Small East: 91 small East : 92 Little East: 93 Little East: 94 Little East: 95 Small East: 96 small East: 97 small East: 98 small East: 99 Xiao Ming: 42 Xiao Ming: 43 Xiao Ming: 44 Xiao Ming: 45 Xiao Ming: 46 Xiao Ming: 47 Xiao Ming: 48 Xiao Ming: 49 Xiao Ming: 50 xiao Ming: 51 Xiao Ming: 52 Xiao Ming: 53 Xiao Ming: 54 Xiao Ming: 55 Xiao Ming : 56 Xiao Ming: 57 Xiao Ming: 58 Xiao Ming: 59 Xiao Ming: 60 Xiao Ming: 61 Xiao Ming: 62 Xiao Ming: 63 Xiao Ming: 64 Xiao Ming: 65 Xiao Ming: 66 Xiao Ming: 67 Xiao Ming: 68 Xiao Ming: 69 Xiao Ming: 70 Xiao Ming: 71 Xiao Ming: 72 Xiao Ming: 73 Xiao Ming: 74 Xiao Ming: 75 xiao Ming: 76 Xiao Ming: 77 Xiao Ming : 78 Xiao Ming: 79 Xiao Ming: 80 Xiao Ming: 81 Xiao Ming: 82 Xiao Ming: 83 Xiao Ming: 84 Xiao Ming: 85 Xiao Ming: 86 Xiao Ming: 87 Xiao Ming: 88 Xiao Ming: 89 Xiao Ming: 90 Xiao Ming: 91 Xiao Ming: 92 Xiao Ming: 93 Xiao Ming: 94 Xiao Ming: 95 Xiao Ming: 96 Xiao Ming: 97 Xiao Ming: 98 Xiao Ming: 99
You can see the execution result as: Xiaoming and small East thread alternate printing
Therefore, the Join method must be called after the thread start method call makes sense.
Three, the realization principle
Public final synchronized void join (long Millis) throws interruptedexception { long base = System.currenttimemillis (); Long now = 0; if (Millis < 0) { throw new IllegalArgumentException ("Timeout value is negative"); } if (Millis = = 0) { while (isAlive ()) { Wait (0), } } else {while (IsAlive ()) { Long delay = mil Lis-now; if (delay <= 0) {break ; } Wait (delay); now = System.currenttimemillis ()-Base;}}}
As can be seen, the principle of the Join method is to invoke the wait method of the corresponding thread waiting for the operation.
Introduction to joins in Threads thread