java线程中的join方法、yield方法和priority线程优先级
测试join方法,合并线程
TestJoin.java
public class TestJoin { public static void main(String[] args) { MyThread2 t1 = new MyThread2("abcde"); t1.start(); try { //join合并线程,将t1,main两个线程,合并到一个线程 t1.join(); } catch (InterruptedException e) {} for(int i=1;i<=10;i++){ System.out.println("i am main thread"); } } } class MyThread2 extends Thread { //给线程命名 MyThread2(String s){ super(s); } public void run(){ for(int i =1;i<=10;i++){ //打印 System.out.println("i am "+getName()); try { sleep(1000); } catch (InterruptedException e) { return; } } } }
F:\java\Thread>javac TestJoin.java F:\java\Thread>java TestJoin i am abcde i am abcde i am abcde i am abcde i am abcde i am abcde i am abcde i am abcde i am abcde i am abcde i am main thread i am main thread i am main thread i am main thread i am main thread i am main thread i am main thread i am main thread i am main thread i am main thread F:\java\Thread>
测试yield,让出线程
TestYield.java
public class TestYield { public static void main(String[] args) { MyThread3 t1 = new MyThread3("t1"); MyThread3 t2 = new MyThread3("t2"); t1.start(); t2.start(); } } class MyThread3 extends Thread { MyThread3(String s){super(s);} public void run(){ for(int i =1;i<=100;i++){ System.out.println(getName()+": "+i); if(i%10==0){ //当i整除10时,让出线程 yield(); } } } }
F:\java\Thread>javac TestYield.java F:\java\Thread>java TestYield t1: 1 t2: 1 t1: 2 t2: 2 t1: 3 t2: 3 t1: 4 t2: 4 t1: 5 t2: 5 t1: 6 t2: 6 t1: 7 t2: 7 t1: 8 t2: 8 t1: 9 t2: 9 t1: 10 t2: 10 t1: 11 t2: 11 t1: 12 t2: 12 t1: 13 t2: 13 t1: 14 t2: 14 t1: 15 t2: 15 t1: 16 t2: 16 t1: 17 t2: 17 t1: 18 t2: 18 t1: 19 t2: 19 t1: 20 t2: 20 t1: 21 t2: 21 t1: 22 t2: 22 t1: 23 t2: 23 t1: 24 t2: 24 t1: 25 t2: 25 t1: 26 t2: 26 t1: 27 t2: 27 t1: 28 t2: 28 t1: 29 t2: 29 t1: 30 t2: 30 t1: 31 t2: 31 t1: 32 t2: 32 t1: 33 t2: 33 t1: 34 t2: 34 t1: 35 t2: 35 t1: 36 t2: 36 t1: 37 t2: 37 t1: 38 t2: 38 t1: 39 t2: 39 t1: 40 t2: 40 t1: 41 t2: 41 t1: 42 t2: 42 t1: 43 t2: 43 t1: 44 t2: 44 t1: 45 t2: 45 t1: 46 t2: 46 t1: 47 t2: 47 t1: 48 t2: 48 t1: 49 t2: 49 t1: 50 t2: 50 t1: 51 t2: 51 t1: 52 t2: 52 t1: 53 t2: 53 t1: 54 t2: 54 t1: 55 t2: 55 t1: 56 t2: 56 t1: 57 t2: 57 t1: 58 t2: 58 t1: 59 t2: 59 t1: 60 t2: 60 t1: 61 t2: 61 t1: 62 t2: 62 t1: 63 t2: 63 t1: 64 t2: 64 t1: 65 t2: 65 t1: 66 t2: 66 t1: 67 t2: 67 t1: 68 t2: 68 t1: 69 t2: 69 t1: 70 t2: 70 t1: 71 t2: 71 t1: 72 t2: 72 t1: 73 t2: 73 t1: 74 t2: 74 t1: 75 t2: 75 t1: 76 t2: 76 t1: 77 t2: 77 t1: 78 t2: 78 t2: 79 t1: 79 t2: 80 t1: 80 t2: 81 t1: 81 t2: 82 t1: 82 t2: 83 t1: 83 t2: 84 t1: 84 t2: 85 t1: 85 t2: 86 t1: 86 t2: 87 t1: 87 t2: 88 t1: 88 t2: 89 t1: 89 t2: 90 t1: 90 t2: 91 t1: 91 t2: 92 t1: 92 t2: 93 t1: 93 t2: 94 t1: 94 t2: 95 t1: 95 t2: 96 t1: 96 t2: 97 t1: 97 t2: 98 t1: 98 t2: 99 t1: 99 t2: 100 t1: 100 F:\java\Thread>
测试线程优先级,
TestPriority.java
/* 测试线程优先级 */ public class TestPriority { public static void main(String[] args) { Thread t1 = new Thread(new T1()); Thread t2 = new Thread(new T2()); //设置线程优先级,Thread.NORM_PRIORITY一般优先级,加3 t1.setPriority(Thread.NORM_PRIORITY + 3); t1.start(); t2.start(); } } class T1 implements Runnable { public void run() { for(int i=0; i<1000; i++) { System.out.println("T1: " + i); } } } class T2 implements Runnable { public void run() { for(int i=0; i<1000; i++) { System.out.println("------T2: " + i); } } }
F:\java\Thread>javac TestPriority.java F:\java\Thread>java TestPriority T1: 0 ------T2: 0 T1: 1 ------T2: 1 T1: 2 ------T2: 2 T1: 3 ------T2: 3 T1: 4 T1: 5 ------T2: 4 T1: 6 ------T2: 5 T1: 7 ------T2: 6 T1: 8 ------T2: 7 T1: 9 ------T2: 8 T1: 10 ------T2: 9 T1: 11 ------T2: 10 T1: 12 ------T2: 11 T1: 13 ------T2: 12 T1: 14 ------T2: 13 T1: 15 ------T2: 14 T1: 16 ------T2: 15 T1: 17 ------T2: 16 T1: 18 ------T2: 17 T1: 19 ------T2: 18 T1: 20 ------T2: 19 T1: 21 ------T2: 20 T1: 22 ------T2: 21 T1: 23 ------T2: 22 T1: 24 ------T2: 23 T1: 25 ------T2: 24 T1: 26 ------T2: 25 T1: 27 ------T2: 26 T1: 28 ------T2: 27 T1: 29 T1: 30 ------T2: 28 T1: 31 ------T2: 29 T1: 32 ------T2: 30 T1: 33 ------T2: 31 T1: 34 ------T2: 32 T1: 35 ------T2: 33 T1: 36 ------T2: 34 T1: 37 ------T2: 35 T1: 38 ------T2: 36 T1: 39 ------T2: 37 T1: 40 ------T2: 38 T1: 41 ------T2: 39 T1: 42 ------T2: 40 T1: 43 ------T2: 41 T1: 44 ------T2: 42 T1: 45 ------T2: 43 T1: 46 ------T2: 44 T1: 47 ------T2: 45 T1: 48 ------T2: 46 T1: 49 ------T2: 47 T1: 50 ------T2: 48 T1: 51 ------T2: 49 T1: 52 ------T2: 50 T1: 53 ------T2: 51 T1: 54 ------T2: 52 T1: 55 ------T2: 53 T1: 56 ------T2: 54 T1: 57 ------T2: 55 T1: 58 ------T2: 56 T1: 59 ------T2: 57 T1: 60 ------T2: 58 T1: 61 ------T2: 59 T1: 62 ------T2: 60 T1: 63 ------T2: 61 T1: 64 T1: 65 ------T2: 62 T1: 66 ------T2: 63 T1: 67 T1: 68 ------T2: 64 T1: 69 ------T2: 65 T1: 70 ------T2: 66 T1: 71 ------T2: 67 T1: 72 ------T2: 68 T1: 73 ------T2: 69 T1: 74 ------T2: 70 T1: 75 ------T2: 71 T1: 76 ------T2: 72 T1: 77 ------T2: 73 T1: 78 ------T2: 74 T1: 79 ------T2: 75 T1: 80 ------T2: 76 T1: 81 ------T2: 77 T1: 82 ------T2: 78 T1: 83 ------T2: 79 T1: 84 ------T2: 80 T1: 85 ------T2: 81 T1: 86 ------T2: 82 T1: 87 ------T2: 83 T1: 88 ------T2: 84 T1: 89 T1: 90 ------T2: 85 T1: 91 ------T2: 86 T1: 92 ------T2: 87 T1: 93 ------T2: 88 T1: 94 ------T2: 89 T1: 95 ------T2: 90 T1: 96 ------T2: 91 T1: 97 ------T2: 92 T1: 98 ------T2: 93 T1: 99 ------T2: 94 T1: 100 ------T2: 95 T1: 101 ------T2: 96 T1: 102 ------T2: 97 T1: 103 T1: 104 ------T2: 98 T1: 105 ------T2: 99 T1: 106 ------T2: 100 T1: 107 ------T2: 101 T1: 108 ------T2: 102 T1: 109 ------T2: 103 T1: 110 ------T2: 104 T1: 111 ------T2: 105 T1: 112 ------T2: 106 T1: 113 ------T2: 107 T1: 114 ------T2: 108 T1: 115 ------T2: 109 T1: 116 ------T2: 110 T1: 117 ------T2: 111 T1: 118 ------T2: 112 T1: 119 ------T2: 113 T1: 120 ------T2: 114 T1: 121 ------T2: 115 T1: 122 ------T2: 116 T1: 123 ------T2: 117 T1: 124 ------T2: 118 T1: 125 ------T2: 119 T1: 126 ------T2: 120 T1: 127 ------T2: 121 T1: 128 ------T2: 122 T1: 129 ------T2: 123 T1: 130 ------T2: 124 T1: 131 ------T2: 125 T1: 132 ------T2: 126 T1: 133 ------T2: 127 T1: 134 ------T2: 128 T1: 135 ------T2: 129 T1: 136 ------T2: 130 T1: 137 ------T2: 131 T1: 138 ------T2: 132 T1: 139 ------T2: 133 T1: 140 ------T2: 134 T1: 141 ------T2: 135 T1: 142 ------T2: 136 T1: 143 ------T2: 137 T1: 144 ------T2: 138 T1: 145 ------T2: 139 T1: 146 ------T2: 140 T1: 147 ------T2: 141 T1: 148 ------T2: 142 T1: 149 ------T2: 143 T1: 150 ------T2: 144 T1: 151 ------T2: 145 T1: 152 ------T2: 146 T1: 153 ------T2: 147 T1: 154 ------T2: 148 T1: 155 ------T2: 149 T1: 156 ------T2: 150 T1: 157 ------T2: 151 T1: 158 ------T2: 152 T1: 159 ------T2: 153 T1: 160 ------T2: 154 T1: 161 ------T2: 155 T1: 162 ------T2: 156 T1: 163 ------T2: 157 T1: 164 ------T2: 158 T1: 165 ------T2: 159 T1: 166 ------T2: 160 T1: 167 ------T2: 161 T1: 168 ------T2: 162 T1: 169 ------T2: 163 T1: 170 ------T2: 164 T1: 171 ------T2: 165 T1: 172 ------T2: 166 T1: 173 ------T2: 167 T1: 174 ------T2: 168 T1: 175 ------T2: 169 T1: 176 ------T2: 170 T1: 177 ------T2: 171 T1: 178 ------T2: 172 T1: 179 ------T2: 173 T1: 180 ------T2: 174 T1: 181 ------T2: 175 T1: 182 ------T2: 176 T1: 183 ------T2: 177 T1: 184 ------T2: 178 T1: 185 ------T2: 179 T1: 186 ------T2: 180 T1: 187 ------T2: 181 T1: 188 ------T2: 182 T1: 189 ------T2: 183 T1: 190 ------T2: 184 T1: 191 ------T2: 185 T1: 192 ------T2: 186 T1: 193 ------T2: 187 T1: 194 ------T2: 188 T1: 195 ------T2: 189 T1: 196 ------T2: 190 T1: 197 ------T2: 191 T1: 198 ------T2: 192 T1: 199 ------T2: 193 T1: 200 ------T2: 194 T1: 201 ------T2: 195 T1: 202 ------T2: 196 T1: 203 ------T2: 197 T1: 204 ------T2: 198 T1: 205 ------T2: 199 T1: 206 ------T2: 200 T1: 207 ------T2: 201 T1: 208 ------T2: 202 T1: 209 ------T2: 203 T1: 210 ------T2: 204 T1: 211 ------T2: 205 T1: 212 ------T2: 206 T1: 213 ------T2: 207 T1: 214 ------T2: 208 T1: 215 ------T2: 209 T1: 216 ------T2: 210 T1: 217 ------T2: 211 T1: 218 ------T2: 212 T1: 219 ------T2: 213 T1: 220 ------T2: 214 T1: 221 ------T2: 215 T1: 222 T1: 223 T1: 224 T1: 225 T1: 226 T1: 227 ------T2: 216 T1: 228 ------T2: 217 T1: 229 ------T2: 218 T1: 230 ------T2: 219 T1: 231 ------T2: 220 T1: 232 ------T2: 221 T1: 233 ------T2: 222 T1: 234 ------T2: 223 T1: 235 ------T2: 224 T1: 236 ------T2: 225 T1: 237 ------T2: 226 T1: 238 ------T2: 227 T1: 239 ------T2: 228 ------T2: 229 T1: 240 ------T2: 230 T1: 241 ------T2: 231 ------T2: 232 ------T2: 233 ------T2: 234 ------T2: 235 T1: 242 ------T2: 236 T1: 243 ------T2: 237 T1: 244 ------T2: 238 T1: 245 ------T2: 239 T1: 246 ------T2: 240 T1: 247 ------T2: 241 T1: 248 ------T2: 242 T1: 249 ------T2: 243 T1: 250 ------T2: 244 T1: 251 ------T2: 245 T1: 252 ------T2: 246 T1: 253 ------T2: 247 T1: 254 ------T2: 248 T1: 255 ------T2: 249 T1: 256 ------T2: 250 T1: 257 ------T2: 251 T1: 258 ------T2: 252 T1: 259 ------T2: 253 T1: 260 ------T2: 254 T1: 261 ------T2: 255 T1: 262 ------T2: 256 T1: 263 ------T2: 257 T1: 264 ------T2: 258 T1: 265 ------T2: 259 T1: 266 ------T2: 260 T1: 267 ------T2: 261 T1: 268 ------T2: 262 T1: 269 ------T2: 263 T1: 270 ------T2: 264 T1: 271 ------T2: 265 T1: 272 ------T2: 266 T1: 273 ------T2: 267 T1: 274 ------T2: 268 T1: 275 T1: 276 ------T2: 269 T1: 277 ------T2: 270 T1: 278 ------T2: 271 T1: 279 ------T2: 272 T1: 280 ------T2: 273 T1: 281 ------T2: 274 T1: 282 ------T2: 275 T1: 283 ------T2: 276 T1: 284 ------T2: 277 T1: 285 ------T2: 278 T1: 286 ------T2: 279 T1: 287 ------T2: 280 T1: 288 T1: 289 ------T2: 281 T1: 290 ------T2: 282 T1: 291 ------T2: 283 ------T2: 284 T1: 292 ------T2: 285 T1: 293 ------T2: 286 T1: 294 ------T2: 287 T1: 295 ------T2: 288 T1: 296 ------T2: 289 T1: 297 ------T2: 290 T1: 298 ------T2: 291 ------T2: 292 T1: 299 ------T2: 293 T1: 300 ------T2: 294 T1: 301 ------T2: 295 T1: 302 ------T2: 296 T1: 303 ------T2: 297 T1: 304 ------T2: 298 T1: 305 ------T2: 299 T1: 306 T1: 307 T1: 308 T1: 309 ------T2: 300 T1: 310 ------T2: 301 T1: 311 ------T2: 302 T1: 312 ------T2: 303 ------T2: 304 ------T2: 305 T1: 313 ------T2: 306 T1: 314 ------T2: 307 T1: 315 ------T2: 308 T1: 316 ------T2: 309 T1: 317 ------T2: 310 T1: 318 ------T2: 311 T1: 319 ------T2: 312 T1: 320 ------T2: 313 T1: 321 ------T2: 314 T1: 322 ------T2: 315 T1: 323 ------T2: 316 T1: 324 T1: 325 ------T2: 317 T1: 326 ------T2: 318 T1: 327 ------T2: 319 ------T2: 320 T1: 328 T1: 329 ------T2: 321 T1: 330 ------T2: 322 T1: 331 ------T2: 323 ------T2: 324 T1: 332 ------T2: 325 T1: 333 ------T2: 326 T1: 334 ------T2: 327 T1: 335 ------T2: 328 T1: 336 ------T2: 329 T1: 337 ------T2: 330 T1: 338 ------T2: 331 ------T2: 332 T1: 339 ------T2: 333 T1: 340 ------T2: 334 T1: 341 ------T2: 335 T1: 342 T1: 343 ------T2: 336 T1: 344 ------T2: 337 ------T2: 338 ------T2: 339 ------T2: 340 ------T2: 341 ------T2: 342 T1: 345 ------T2: 343 T1: 346 ------T2: 344 T1: 347 ------T2: 345 T1: 348 T1: 349 ------T2: 346 T1: 350 ------T2: 347 T1: 351 ------T2: 348 T1: 352 ------T2: 349 T1: 353 ------T2: 350 T1: 354 ------T2: 351 T1: 355 ------T2: 352 T1: 356 ------T2: 353 T1: 357 T1: 358 ------T2: 354 T1: 359 ------T2: 355 T1: 360 T1: 361 ------T2: 356 T1: 362 ------T2: 357 T1: 363 ------T2: 358 ------T2: 359 ------T2: 360 T1: 364 ------T2: 361 T1: 365 T1: 366 ------T2: 362 ------T2: 363 T1: 367 T1: 368 ------T2: 364 T1: 369 ------T2: 365 T1: 370 ------T2: 366 T1: 371 ------T2: 367 T1: 372 ------T2: 368 T1: 373 ------T2: 369 T1: 374 ------T2: 370 T1: 375 T1: 376 T1: 377 ------T2: 371 T1: 378 ------T2: 372 T1: 379 ------T2: 373 T1: 380 ------T2: 374 T1: 381 ------T2: 375 T1: 382 ------T2: 376 T1: 383 ------T2: 377 T1: 384 ------T2: 378 T1: 385 ------T2: 379 T1: 386 ------T2: 380 T1: 387 T1: 388 T1: 389 T1: 390 T1: 391 T1: 392 T1: 393 T1: 394 ------T2: 381 T1: 395 ------T2: 382 T1: 396 ------T2: 383 T1: 397 ------T2: 384 T1: 398 ------T2: 385 T1: 399 ------T2: 386 T1: 400 ------T2: 387 T1: 401 ------T2: 388 ------T2: 389 T1: 402 ------T2: 390 T1: 403 ------T2: 391 T1: 404 T1: 405 ------T2: 392 T1: 406 ------T2: 393 T1: 407 T1: 408 ------T2: 394 T1: 409 ------T2: 395 T1: 410 ------T2: 396 T1: 411 ------T2: 397 T1: 412 T1: 413 ------T2: 398 T1: 414 ------T2: 399 T1: 415 ------T2: 400 T1: 416 ------T2: 401 T1: 417 ------T2: 402 T1: 418 ------T2: 403 ------T2: 404 T1: 419 ------T2: 405 T1: 420 ------T2: 406 T1: 421 ------T2: 407 T1: 422 ------T2: 408 T1: 423 ------T2: 409 T1: 424 ------T2: 410 T1: 425 ------T2: 411 T1: 426 ------T2: 412 T1: 427 ------T2: 413 T1: 428 ------T2: 414 T1: 429 ------T2: 415 T1: 430 T1: 431 ------T2: 416 T1: 432 ------T2: 417 T1: 433 ------T2: 418 T1: 434 ------T2: 419 T1: 435 ------T2: 420 T1: 436 ------T2: 421 T1: 437 ------T2: 422 T1: 438 ------T2: 423 T1: 439 ------T2: 424 T1: 440 ------T2: 425 T1: 441 ------T2: 426 T1: 442 ------T2: 427 T1: 443 ------T2: 428 T1: 444 T1: 445 ------T2: 429 T1: 446 T1: 447 ------T2: 430 T1: 448 ------T2: 431 T1: 449 ------T2: 432 T1: 450 ------T2: 433 ------T2: 434 T1: 451 ------T2: 435 T1: 452 ------T2: 436 T1: 453 T1: 454 ------T2: 437 T1: 455 ------T2: 438 T1: 456 ------T2: 439 T1: 457 T1: 458 ------T2: 440 T1: 459 T1: 460 T1: 461 T1: 462 T1: 463 ------T2: 441 T1: 464 T1: 465 T1: 466 T1: 467 T1: 468 ------T2: 442 T1: 469 ------T2: 443 T1: 470 ------T2: 444 T1: 471 ------T2: 445 T1: 472 ------T2: 446 T1: 473 ------T2: 447 T1: 474 ------T2: 448 T1: 475 ------T2: 449 T1: 476 ------T2: 450 T1: 477 T1: 478 T1: 479 ------T2: 451 T1: 480 ------T2: 452 T1: 481 ------T2: 453 T1: 482 ------T2: 454 T1: 483 ------T2: 455 T1: 484 ------T2: 456 T1: 485 T1: 486 ------T2: 457 T1: 487 ------T2: 458 T1: 488 ------T2: 459 T1: 489 ------T2: 460 T1: 490 ------T2: 461 T1: 491 ------T2: 462 T1: 492 ------T2: 463 T1: 493 ------T2: 464 T1: 494 ------T2: 465 T1: 495 ------T2: 466 T1: 496 ------T2: 467 T1: 497 T1: 498 ------T2: 468 T1: 499 ------T2: 469 T1: 500 ------T2: 470 T1: 501 ------T2: 471 T1: 502 ------T2: 472 T1: 503 ------T2: 473 T1: 504 ------T2: 474 T1: 505 ------T2: 475 T1: 506 ------T2: 476 T1: 507 ------T2: 477 T1: 508 ------T2: 478 T1: 509 ------T2: 479 T1: 510 ------T2: 480 T1: 511 ------T2: 481 T1: 512 ------T2: 482 T1: 513 ------T2: 483 T1: 514 ------T2: 484 T1: 515 ------T2: 485 T1: 516 ------T2: 486 T1: 517 T1: 518 ------T2: 487 T1: 519 ------T2: 488 T1: 520 ------T2: 489 T1: 521 ------T2: 490 T1: 522 T1: 523 ------T2: 491 T1: 524 ------T2: 492 T1: 525 ------T2: 493 T1: 526 ------T2: 494 T1: 527 ------T2: 495 T1: 528 ------T2: 496 T1: 529 ------T2: 497 T1: 530 ------T2: 498 T1: 531 ------T2: 499 T1: 532 ------T2: 500 T1: 533 ------T2: 501 T1: 534 ------T2: 502 ------T2: 503 ------T2: 504 T1: 535 T1: 536 ------T2: 505 T1: 537 ------T2: 506 T1: 538 ------T2: 507 T1: 539 ------T2: 508 T1: 540 ------T2: 509 T1: 541 ------T2: 510 T1: 542 ------T2: 511 T1: 543 T1: 544 ------T2: 512 T1: 545 ------T2: 513 T1: 546 ------T2: 514 T1: 547 T1: 548 T1: 549 ------T2: 515 T1: 550 ------T2: 516 T1: 551 ------T2: 517 T1: 552 ------T2: 518 T1: 553 ------T2: 519 T1: 554 ------T2: 520 T1: 555 ------T2: 521 T1: 556 T1: 557 ------T2: 522 T1: 558 ------T2: 523 T1: 559 ------T2: 524 T1: 560 ------T2: 525 T1: 561 ------T2: 526 T1: 562 ------T2: 527 T1: 563 ------T2: 528 T1: 564 T1: 565 ------T2: 529 T1: 566 T1: 567 ------T2: 530 T1: 568 ------T2: 531 T1: 569 ------T2: 532 T1: 570 ------T2: 533 T1: 571 ------T2: 534 T1: 572 ------T2: 535 T1: 573 ------T2: 536 T1: 574 ------T2: 537 T1: 575 T1: 576 ------T2: 538 T1: 577 ------T2: 539 T1: 578 ------T2: 540 T1: 579 ------T2: 541 ------T2: 542 T1: 580 ------T2: 543 T1: 581 ------T2: 544 T1: 582 T1: 583 T1: 584 ------T2: 545 T1: 585 ------T2: 546 T1: 586 ------T2: 547 T1: 587 T1: 588 ------T2: 548 T1: 589 ------T2: 549 T1: 590 ------T2: 550 T1: 591 ------T2: 551 T1: 592 ------T2: 552 T1: 593 T1: 594 T1: 595 ------T2: 553 T1: 596 ------T2: 554 T1: 597 ------T2: 555 T1: 598 ------T2: 556 T1: 599 ------T2: 557 T1: 600 ------T2: 558 T1: 601 ------T2: 559 T1: 602 ------T2: 560 ------T2: 561 T1: 603 ------T2: 562 T1: 604 ------T2: 563 T1: 605 ------T2: 564 ------T2: 565 T1: 606 ------T2: 566 T1: 607 ------T2: 567 T1: 608 ------T2: 568 T1: 609 ------T2: 569 T1: 610 ------T2: 570 T1: 611 ------T2: 571 T1: 612 ------T2: 572 T1: 613 ------T2: 573 T1: 614 ------T2: 574 T1: 615 ------T2: 575 T1: 616 T1: 617 ------T2: 576 T1: 618 ------T2: 577 T1: 619 ------T2: 578 T1: 620 ------T2: 579 T1: 621 ------T2: 580 T1: 622 ------T2: 581 T1: 623 ------T2: 582 T1: 624 ------T2: 583 T1: 625 ------T2: 584 T1: 626 ------T2: 585 T1: 627 ------T2: 586 T1: 628 ------T2: 587 T1: 629 ------T2: 588 T1: 630 ------T2: 589 T1: 631 ------T2: 590 T1: 632 ------T2: 591 T1: 633 ------T2: 592 T1: 634 ------T2: 593 T1: 635 ------T2: 594 T1: 636 ------T2: 595 T1: 637 ------T2: 596 T1: 638 ------T2: 597 T1: 639 ------T2: 598 T1: 640 ------T2: 599 T1: 641 ------T2: 600 T1: 642 ------T2: 601 T1: 643 ------T2: 602 T1: 644 T1: 645 ------T2: 603 T1: 646 ------T2: 604 T1: 647 ------T2: 605 T1: 648 ------T2: 606 T1: 649 ------T2: 607 T1: 650 ------T2: 608 T1: 651 ------T2: 609 T1: 652 ------T2: 610 T1: 653 ------T2: 611 T1: 654 ------T2: 612 T1: 655 ------T2: 613 T1: 656 T1: 657 ------T2: 614 T1: 658 ------T2: 615 T1: 659 ------T2: 616 T1: 660 ------T2: 617 ------T2: 618 ------T2: 619 T1: 661 ------T2: 620 T1: 662 ------T2: 621 T1: 663 T1: 664 T1: 665 ------T2: 622 T1: 666 ------T2: 623 ------T2: 624 T1: 667 ------T2: 625 T1: 668 ------T2: 626 T1: 669 ------T2: 627 T1: 670 ------T2: 628 T1: 671 ------T2: 629 T1: 672 ------T2: 630 T1: 673 ------T2: 631 T1: 674 ------T2: 632 T1: 675 ------T2: 633 T1: 676 ------T2: 634 T1: 677 ------T2: 635 T1: 678 ------T2: 636 T1: 679 ------T2: 637 T1: 680 ------T2: 638 T1: 681 ------T2: 639 T1: 682 ------T2: 640 T1: 683 ------T2: 641 T1: 684 ------T2: 642 T1: 685 ------T2: 643 T1: 686 ------T2: 644 T1: 687 ------T2: 645 T1: 688 ------T2: 646 T1: 689 ------T2: 647 T1: 690 T1: 691 ------T2: 648 T1: 692 ------T2: 649 T1: 693 ------T2: 650 T1: 694 ------T2: 651 ------T2: 652 T1: 695 ------T2: 653 T1: 696 ------T2: 654 T1: 697 ------T2: 655 T1: 698 ------T2: 656 T1: 699 ------T2: 657 T1: 700 T1: 701 ------T2: 658 T1: 702 ------T2: 659 T1: 703 ------T2: 660 T1: 704 ------T2: 661 T1: 705 ------T2: 662 T1: 706 ------T2: 663 T1: 707 ------T2: 664 T1: 708 T1: 709 ------T2: 665 ------T2: 666 ------T2: 667 ------T2: 668 ------T2: 669 ------T2: 670 T1: 710 T1: 711 ------T2: 671 ------T2: 672 ------T2: 673 ------T2: 674 ------T2: 675 ------T2: 676 T1: 712 ------T2: 677 T1: 713 ------T2: 678 ------T2: 679 T1: 714 ------T2: 680 T1: 715 ------T2: 681 T1: 716 ------T2: 682 T1: 717 ------T2: 683 T1: 718 ------T2: 684 T1: 719 ------T2: 685 T1: 720 ------T2: 686 T1: 721 ------T2: 687 T1: 722 ------T2: 688 T1: 723 ------T2: 689 T1: 724 T1: 725 ------T2: 690 T1: 726 ------T2: 691 T1: 727 ------T2: 692 T1: 728 ------T2: 693 T1: 729 ------T2: 694 T1: 730 T1: 731 ------T2: 695 T1: 732 ------T2: 696 T1: 733 ------T2: 697 T1: 734 ------T2: 698 T1: 735 ------T2: 699 T1: 736 ------T2: 700 T1: 737 ------T2: 701 T1: 738 ------T2: 702 T1: 739 ------T2: 703 T1: 740 ------T2: 704 T1: 741 ------T2: 705 T1: 742 ------T2: 706 T1: 743 ------T2: 707 T1: 744 ------T2: 708 T1: 745 ------T2: 709 T1: 746 ------T2: 710 T1: 747 T1: 748 T1: 749 T1: 750 T1: 751 ------T2: 711 T1: 752 ------T2: 712 T1: 753 ------T2: 713 T1: 754 ------T2: 714 T1: 755 ------T2: 715 T1: 756 ------T2: 716 T1: 757 ------T2: 717 ------T2: 718 ------T2: 719 ------T2: 720 T1: 758 ------T2: 721 T1: 759 ------T2: 722 T1: 760 T1: 761 T1: 762 T1: 763 T1: 764 T1: 765 ------T2: 723 T1: 766 ------T2: 724 T1: 767 T1: 768 ------T2: 725 T1: 769 ------T2: 726 T1: 770 ------T2: 727 T1: 771 ------T2: 728 T1: 772 ------T2: 729 T1: 773 ------T2: 730 T1: 774 ------T2: 731 ------T2: 732 ------T2: 733 ------T2: 734 T1: 775 ------T2: 735 T1: 776 ------T2: 736 T1: 777 ------T2: 737 T1: 778 ------T2: 738 T1: 779 ------T2: 739 T1: 780 ------T2: 740 T1: 781 ------T2: 741 T1: 782 ------T2: 742 T1: 783 ------T2: 743 T1: 784 ------T2: 744 T1: 785 ------T2: 745 T1: 786 T1: 787 ------T2: 746 T1: 788 ------T2: 747 T1: 789 ------T2: 748 T1: 790 T1: 791 ------T2: 749 T1: 792 ------T2: 750 T1: 793 ------T2: 751 T1: 794 ------T2: 752 ------T2: 753 T1: 795 ------T2: 754 T1: 796 ------T2: 755 T1: 797 ------T2: 756 T1: 798 ------T2: 757 T1: 799 ------T2: 758 T1: 800 ------T2: 759 T1: 801 ------T2: 760 T1: 802 ------T2: 761 T1: 803 ------T2: 762 T1: 804 ------T2: 763 T1: 805 ------T2: 764 T1: 806 ------T2: 765 T1: 807 ------T2: 766 T1: 808 ------T2: 767 T1: 809 ------T2: 768 T1: 810 ------T2: 769 T1: 811 ------T2: 770 T1: 812 ------T2: 771 T1: 813 ------T2: 772 T1: 814 ------T2: 773 T1: 815 ------T2: 774 T1: 816 ------T2: 775 T1: 817 T1: 818 ------T2: 776 T1: 819 ------T2: 777 T1: 820 ------T2: 778 T1: 821 ------T2: 779 T1: 822 ------T2: 780 T1: 823 ------T2: 781 ------T2: 782 T1: 824 ------T2: 783 T1: 825 T1: 826 ------T2: 784 ------T2: 785 T1: 827 T1: 828 ------T2: 786 T1: 829 ------T2: 787 T1: 830 ------T2: 788 ------T2: 789 ------T2: 790 ------T2: 791 ------T2: 792 T1: 831 T1: 832 T1: 833 T1: 834 T1: 835 T1: 836 T1: 837 T1: 838 T1: 839 ------T2: 793 T1: 840 ------T2: 794 ------T2: 795 T1: 841 ------T2: 796 T1: 842 ------T2: 797 T1: 843 ------T2: 798 T1: 844 ------T2: 799 T1: 845 ------T2: 800 T1: 846 ------T2: 801 T1: 847 ------T2: 802 T1: 848 ------T2: 803 T1: 849 ------T2: 804 T1: 850 ------T2: 805 T1: 851 ------T2: 806 T1: 852 ------T2: 807 T1: 853 ------T2: 808 T1: 854 ------T2: 809 T1: 855 ------T2: 810 T1: 856 ------T2: 811 T1: 857 ------T2: 812 T1: 858 ------T2: 813 T1: 859 ------T2: 814 T1: 860 ------T2: 815 T1: 861 ------T2: 816 T1: 862 ------T2: 817 T1: 863 ------T2: 818 ------T2: 819 T1: 864 ------T2: 820 T1: 865 ------T2: 821 T1: 866 ------T2: 822 T1: 867 ------T2: 823 T1: 868 ------T2: 824 T1: 869 ------T2: 825 T1: 870 ------T2: 826 T1: 871 ------T2: 827 T1: 872 ------T2: 828 T1: 873 ------T2: 829 T1: 874 ------T2: 830 T1: 875 T1: 876 ------T2: 831 T1: 877 T1: 878 T1: 879 T1: 880 ------T2: 832 T1: 881 ------T2: 833 T1: 882 ------T2: 834 T1: 883 ------T2: 835 T1: 884 ------T2: 836 T1: 885 ------T2: 837 T1: 886 ------T2: 838 T1: 887 ------T2: 839 T1: 888 ------T2: 840 T1: 889 ------T2: 841 T1: 890 ------T2: 842 T1: 891 ------T2: 843 T1: 892 ------T2: 844 T1: 893 ------T2: 845 T1: 894 ------T2: 846 T1: 895 ------T2: 847 T1: 896 ------T2: 848 T1: 897 ------T2: 849 T1: 898 ------T2: 850 T1: 899 ------T2: 851 T1: 900 ------T2: 852 T1: 901 T1: 902 ------T2: 853 T1: 903 ------T2: 854 T1: 904 ------T2: 855 ------T2: 856 T1: 905 ------T2: 857 ------T2: 858 T1: 906 T1: 907 T1: 908 T1: 909 T1: 910 T1: 911 ------T2: 859 T1: 912 T1: 913 ------T2: 860 T1: 914 ------T2: 861 T1: 915 T1: 916 ------T2: 862 T1: 917 ------T2: 863 T1: 918 ------T2: 864 T1: 919 ------T2: 865 T1: 920 ------T2: 866 T1: 921 ------T2: 867 T1: 922 ------T2: 868 T1: 923 ------T2: 869 T1: 924 ------T2: 870 T1: 925 ------T2: 871 T1: 926 ------T2: 872 T1: 927 ------T2: 873 T1: 928 T1: 929 ------T2: 874 T1: 930 ------T2: 875 T1: 931 ------T2: 876 T1: 932 ------T2: 877 T1: 933 ------T2: 878 T1: 934 ------T2: 879 T1: 935 ------T2: 880 T1: 936 T1: 937 ------T2: 881 T1: 938 ------T2: 882 T1: 939 ------T2: 883 T1: 940 ------T2: 884 T1: 941 ------T2: 885 T1: 942 ------T2: 886 ------T2: 887 ------T2: 888 ------T2: 889 T1: 943 ------T2: 890 ------T2: 891 T1: 944 T1: 945 ------T2: 892 T1: 946 ------T2: 893 T1: 947 ------T2: 894 T1: 948 ------T2: 895 T1: 949 ------T2: 896 T1: 950 ------T2: 897 T1: 951 ------T2: 898 T1: 952 T1: 953 T1: 954 ------T2: 899 T1: 955 ------T2: 900 T1: 956 T1: 957 T1: 958 T1: 959 T1: 960 ------T2: 901 T1: 961 ------T2: 902 T1: 962 ------T2: 903 T1: 963 ------T2: 904 T1: 964 ------T2: 905 T1: 965 ------T2: 906 T1: 966 ------T2: 907 T1: 967 ------T2: 908 T1: 968 ------T2: 909 T1: 969 ------T2: 910 T1: 970 ------T2: 911 T1: 971 ------T2: 912 T1: 972 ------T2: 913 T1: 973 ------T2: 914 T1: 974 ------T2: 915 T1: 975 ------T2: 916 T1: 976 ------T2: 917 T1: 977 ------T2: 918 T1: 978 ------T2: 919 T1: 979 ------T2: 920 T1: 980 ------T2: 921 T1: 981 ------T2: 922 T1: 982 ------T2: 923 T1: 983 ------T2: 924 T1: 984 ------T2: 925 T1: 985 ------T2: 926 T1: 986 ------T2: 927 T1: 987 ------T2: 928 T1: 988 ------T2: 929 T1: 989 ------T2: 930 T1: 990 ------T2: 931 T1: 991 ------T2: 932 T1: 992 T1: 993 ------T2: 933 T1: 994 ------T2: 934 T1: 995 ------T2: 935 T1: 996 ------T2: 936 T1: 997 ------T2: 937 T1: 998 ------T2: 938 T1: 999 ------T2: 939 ------T2: 940 ------T2: 941 ------T2: 942 ------T2: 943 ------T2: 944 ------T2: 945 ------T2: 946 ------T2: 947 ------T2: 948 ------T2: 949 ------T2: 950 ------T2: 951 ------T2: 952 ------T2: 953 ------T2: 954 ------T2: 955 ------T2: 956 ------T2: 957 ------T2: 958 ------T2: 959 ------T2: 960 ------T2: 961 ------T2: 962 ------T2: 963 ------T2: 964 ------T2: 965 ------T2: 966 ------T2: 967 ------T2: 968 ------T2: 969 ------T2: 970 ------T2: 971 ------T2: 972 ------T2: 973 ------T2: 974 ------T2: 975 ------T2: 976 ------T2: 977 ------T2: 978 ------T2: 979 ------T2: 980 ------T2: 981 ------T2: 982 ------T2: 983 ------T2: 984 ------T2: 985 ------T2: 986 ------T2: 987 ------T2: 988 ------T2: 989 ------T2: 990 ------T2: 991 ------T2: 992 ------T2: 993 ------T2: 994 ------T2: 995 ------T2: 996 ------T2: 997 ------T2: 998 ------T2: 999 F:\java\Thread>
相关推荐
7. **Java线程API**:了解如何使用`Thread`类的方法如`start()`, `join()`, `sleep()`, `yield()`, 和 `interrupt()`来控制线程的行为和交互,这些都是多线程编程的基础。 8. **线程池**:线程池是一种高效的线程...
本文将深入探讨Java中的`join()`方法以及线程的优先级概念。 首先,我们来看`join()`方法。`join()`方法用于线程同步,当一个线程(例如线程A)调用另一个线程(例如线程B)的`join()`方法时,线程A会暂停执行,...
在Java中,实现多线程有两种主要方式:继承Thread类和实现Runnable接口。 1. 继承Thread类: 当我们创建一个新的类,让它继承Thread类时,可以通过重写`run()`方法来定义线程执行的任务。然后创建该类的对象,并...
理解并熟练掌握Thread类的构造器和方法,以及线程优先级的概念,对于编写高效、稳定的多线程程序至关重要。在实际开发中,应避免使用已过时的方法,合理利用线程调度机制,确保线程安全,提高程序性能。同时,对守护...
线程的优先级可以通过`getPriority()`获取,Java中线程优先级范围是1(`Thread.MIN_PRIORITY`)到10(`Thread.MAX_PRIORITY`),默认优先级是5(`Thread.NORM_PRIORITY`)。`setPriority()`可以设置线程的优先级,但...
本篇文章将详细探讨如何在Java中启动线程,以及相关的重要概念和技术。 1. **线程创建方式** - **实现Runnable接口**:这是最常见的创建线程的方式,只需创建一个类实现`Runnable`接口,然后在`run()`方法中编写...
在 Java 中,线程的优先级可以通过 setPriority 方法设置,默认的优先级是 5。线程的优先级越高,它的执行机会越高。 在 Java 中,线程的同步可以通过 synchronized 关键字实现。synchronized 关键字可以用于方法或...
- Java中线程有10个优先级,`Thread.NORM_PRIORITY`为默认优先级,`Thread.MIN_PRIORITY`和`Thread.MAX_PRIORITY`分别是最低和最高优先级。 - 优先级高的线程并不保证一定先执行,JVM和操作系统调度策略会影响实际...
- Java的`Thread`类提供了设置线程优先级的方法,如`setPriority()`,但优先级并不保证绝对的执行顺序,只能影响调度概率,实际执行顺序依赖于JVM和操作系统。 7. **`Thread.yield()`**: - `Thread.yield()`方法...
线程优先级的调整可以通过`setPriority()`和`getPriority()`方法实现。Java定义了三个预定义的优先级:`MAX_PRIORITY`(10)、`NORM_PRIORITY`(5)和`MIN_PRIORITY`(1)。通常,使用这三个常量来设置线程优先级,...
本篇将深入探讨Java线程的操作方法,帮助你理解和掌握这一关键知识点。 1. **线程的创建** - **继承Thread类**:创建一个新的类,该类继承自`Thread`类,并重写`run()`方法。然后创建该类的实例并调用`start()`...
线程的生命周期可以通过start()、run()、sleep()、join()、yield()、interrupt()等方法进行控制。 三、同步机制 1. synchronized关键字:用于方法或代码块,保证在同一时刻,只有一个线程访问特定的代码段,避免...
- 线程可以通过调用特定的方法改变其状态,例如 `start()`、`sleep()`、`join()`、`wait()` 和 `notify()` 等。 #### 五、Java线程:线程的同步与锁 1. **线程同步** - 线程同步是指控制多个线程对共享资源的访问...
- 线程优先级范围是1到10,默认为5。 ```java Thread t = new Thread(); t.setPriority(Thread.MAX_PRIORITY); // 设置最高优先级 t.setPriority(Thread.MIN_PRIORITY); // 设置最低优先级 ``` 2. **让出CPU...
sleep()、yield()和join()是Thread类中的三个与线程调度相关的静态方法。sleep()方法使当前线程暂停执行指定的时间,时间结束后线程将自动进入可运行状态;yield()方法使当前线程让出CPU的使用权,但并不会阻塞,它...
3. **线程控制**:可以使用join()方法使当前线程等待其他线程结束,sleep()方法使当前线程暂停一段时间,yield()方法让当前线程放弃CPU使用权。 三、线程同步 1. **同步机制**:为了解决多线程环境下共享数据的...
本知识点将深入探讨Java多线程编程模板,帮助开发者理解和掌握如何在Java中创建、管理和同步线程。 一、线程的创建 在Java中,有两种主要的线程创建方式: 1. 继承Thread类:创建一个新的类,该类继承自Thread类,...
线程的优先级也是Java线程编程中的一个重要概念,Java定义了10个线程优先级,从MIN_PRIORITY(1)到MAX_PRIORITY(10),默认优先级是NORM_PRIORITY(5)。不过,线程调度器可能会根据系统情况调整优先级,因此优先级并不...
在Java中,线程是程序执行的最小单元,每个线程都有自己的程序计数器、虚拟机栈、本地方法栈和一部分堆内存。线程共享同一进程的内存空间,这意味着它们可以访问相同的数据,但也有潜在的并发问题,如数据竞争和死锁...
- `getPriority()`和`setPriority(int p)`:获取和设置线程优先级。 - `join()`:等待线程结束,如果被中断则抛出异常。 **线程的生命周期**: Java线程有几种状态,包括新生儿(Newborn)、就绪(Runnable)、...