线程的后台线程

后台线程是在后台运行的,他的任务是为其他的线程提供服务,也成为“守护线程”。JVM的垃圾回收线程就是典型的后台线程。
特征:如果所有的前台线程都死亡,那么后台线程会自动死亡。
调用THREAD对象的setDaemon(true)方法就可将制定线程设置为后台线程。

程序:
package com.test;

public class DaemonThread extends Thread {

public void run()
{
for(int i=0;i<1000;i++)
{
System.out.println(Thread.currentThread().getName()+" "+i);
}
}

public static void main(String[] args)
{
DaemonThread dt = new DaemonThread();
dt.setDaemon(true);
dt.start();

for(int i=0;i<10;i++)
{

System.out.println(Thread.currentThread().getName()+" "+i);

}
}
}

打印结果:
main 0
Thread-0 0
main 1
Thread-0 1
main 2
Thread-0 2
Thread-0 3
Thread-0 4
Thread-0 5
main 3
Thread-0 6
main 4
Thread-0 7
main 5
Thread-0 8
main 6
main 7
main 8
main 9
Thread-0 9
Thread-0 10
Thread-0 11
Thread-0 12
Thread-0 13
Thread-0 14
Thread-0 15
Thread-0 16
Thread-0 17
Thread-0 18
Thread-0 19
Thread-0 20
Thread-0 21
Thread-0 22
Thread-0 23
Thread-0 24
Thread-0 25
Thread-0 26
Thread-0 27
Thread-0 28
Thread-0 29
Thread-0 30
Thread-0 31
Thread-0 32
Thread-0 33
Thread-0 34
Thread-0 35
Thread-0 36
Thread-0 37
Thread-0 38
Thread-0 39
Thread-0 40
Thread-0 41
Thread-0 42
Thread-0 43
Thread-0 44
Thread-0 45
Thread-0 46
Thread-0 47
Thread-0 48
Thread-0 49
Thread-0 50
Thread-0 51
Thread-0 52
Thread-0 53
Thread-0 54
Thread-0 55
Thread-0 56
Thread-0 57
Thread-0 58
Thread-0 59
Thread-0 60
Thread-0 61
Thread-0 62
Thread-0 63
Thread-0 64
Thread-0 65
Thread-0 66
Thread-0 67
Thread-0 68
Thread-0 69
Thread-0 70
Thread-0 71
Thread-0 72
Thread-0 73
Thread-0 74
Thread-0 75
Thread-0 76
Thread-0 77
Thread-0 78
Thread-0 79
Thread-0 80
Thread-0 81
Thread-0 82
Thread-0 83
Thread-0 84
Thread-0 85
Thread-0 86
Thread-0 87
Thread-0 88
Thread-0 89
Thread-0 90
Thread-0 91
Thread-0 92
Thread-0 93
Thread-0 94
Thread-0 95
Thread-0 96
Thread-0 97
Thread-0 98
Thread-0 99
Thread-0 100
Thread-0 101
Thread-0 102
Thread-0 103
Thread-0 104
Thread-0 105
Thread-0 106
Thread-0 107
Thread-0 108
Thread-0 109
Thread-0 110
Thread-0 111
Thread-0 112
Thread-0 113
Thread-0 114
Thread-0 115
Thread-0 116
Thread-0 117
Thread-0 118
Thread-0 119
Thread-0 120
Thread-0 121
Thread-0 122
Thread-0 123
Thread-0 124
Thread-0 125
Thread-0 126
Thread-0 127
Thread-0 128
Thread-0 129
Thread-0 130
Thread-0 131
Thread-0 132
Thread-0 133
Thread-0 134
Thread-0 135
Thread-0 136
Thread-0 137
Thread-0 138
Thread-0 139
Thread-0 140
Thread-0 141
Thread-0 142
Thread-0 143
Thread-0 144
Thread-0 145
Thread-0 146
Thread-0 147
Thread-0 148
Thread-0 149
Thread-0 150
Thread-0 151
Thread-0 152
Thread-0 153
Thread-0 154
Thread-0 155
Thread-0 156
Thread-0 157
Thread-0 158
Thread-0 159
Thread-0 160
Thread-0 161
Thread-0 162
Thread-0 163
Thread-0 164
Thread-0 165
Thread-0 166
Thread-0 167
Thread-0 168
Thread-0 169
Thread-0 170
Thread-0 171
Thread-0 172
Thread-0 173
Thread-0 174
Thread-0 175
Thread-0 176
Thread-0 177
Thread-0 178
Thread-0 179
Thread-0 180
Thread-0 181
Thread-0 182
Thread-0 183
Thread-0 184
Thread-0 185
Thread-0 186
Thread-0 187
Thread-0 188
Thread-0 189
Thread-0 190
Thread-0 191
Thread-0 192
Thread-0 193
Thread-0 194
Thread-0 195
Thread-0 196
Thread-0 197
Thread-0 198
Thread-0 199
Thread-0 200
Thread-0 201
Thread-0 202
Thread-0 203
Thread-0 204
Thread-0 205
Thread-0 206
Thread-0 207
Thread-0 208
Thread-0 209
Thread-0 210
Thread-0 211
Thread-0 212
Thread-0 213
Thread-0 214
Thread-0 215
Thread-0 216
Thread-0 217
Thread-0 218
Thread-0 219
Thread-0 220
Thread-0 221
Thread-0 222
Thread-0 223
Thread-0 224
Thread-0 225
Thread-0 226
Thread-0 227
Thread-0 228
Thread-0 229
Thread-0 230
Thread-0 231
Thread-0 232
Thread-0 233
Thread-0 234
Thread-0 235
Thread-0 236
Thread-0 237
Thread-0 238
Thread-0 239
Thread-0 240
Thread-0 241
Thread-0 242
Thread-0 243
Thread-0 244
Thread-0 245
Thread-0 246
Thread-0 247
Thread-0 248
Thread-0 249
Thread-0 250
Thread-0 251
Thread-0 252
Thread-0 253
Thread-0 254
Thread-0 255
Thread-0 256
Thread-0 257
Thread-0 258
Thread-0 259
Thread-0 260
Thread-0 261
Thread-0 262
Thread-0 263
Thread-0 264
Thread-0 265
Thread-0 266
Thread-0 267
Thread-0 268
Thread-0 269
Thread-0 270
Thread-0 271
Thread-0 272
Thread-0 273
Thread-0 274
Thread-0 275
Thread-0 276
Thread-0 277
Thread-0 278
Thread-0 279
Thread-0 280
Thread-0 281
Thread-0 282
Thread-0 283
Thread-0 284
Thread-0 285
Thread-0 286
Thread-0 287
Thread-0 288
Thread-0 289
Thread-0 290
Thread-0 291
Thread-0 292
Thread-0 293
Thread-0 294
Thread-0 295
Thread-0 296
Thread-0 297
Thread-0 298
Thread-0 299
Thread-0 300
Thread-0 301
Thread-0 302
Thread-0 303
Thread-0 304
Thread-0 305
Thread-0 306
Thread-0 307
Thread-0 308
Thread-0 309
Thread-0 310
Thread-0 311
Thread-0 312
Thread-0 313
Thread-0 314
Thread-0 315
Thread-0 316
Thread-0 317
Thread-0 318
Thread-0 319
Thread-0 320
Thread-0 321
Thread-0 322
Thread-0 323
Thread-0 324
Thread-0 325
Thread-0 326
Thread-0 327
Thread-0 328
Thread-0 329
Thread-0 330
Thread-0 331
Thread-0 332
Thread-0 333
Thread-0 334
Thread-0 335
Thread-0 336
Thread-0 337
Thread-0 338
Thread-0 339
Thread-0 340
Thread-0 341
Thread-0 342
Thread-0 343
Thread-0 344
Thread-0 345
Thread-0 346
Thread-0 347
Thread-0 348
Thread-0 349
Thread-0 350
Thread-0 351
Thread-0 352
Thread-0 353
Thread-0 354
Thread-0 355
Thread-0 356
Thread-0 357
Thread-0 358
Thread-0 359
Thread-0 360
Thread-0 361
Thread-0 362
Thread-0 363
Thread-0 364
Thread-0 365
Thread-0 366
Thread-0 367
Thread-0 368
Thread-0 369
Thread-0 370
Thread-0 371
Thread-0 372
Thread-0 373
Thread-0 374
Thread-0 375
Thread-0 376
Thread-0 377
Thread-0 378
Thread-0 379
Thread-0 380
Thread-0 381
Thread-0 382
Thread-0 383
Thread-0 384
Thread-0 385
Thread-0 386
Thread-0 387
Thread-0 388
Thread-0 389
Thread-0 390
Thread-0 391
Thread-0 392
Thread-0 393
Thread-0 394
Thread-0 395
Thread-0 396
Thread-0 397
Thread-0 398
Thread-0 399
Thread-0 400
Thread-0 401
Thread-0 402
Thread-0 403
Thread-0 404
Thread-0 405
Thread-0 406
Thread-0 407
Thread-0 408
Thread-0 409
Thread-0 410
Thread-0 411
Thread-0 412
Thread-0 413
Thread-0 414
Thread-0 415
Thread-0 416
Thread-0 417


我们设置DaemonThread为后台线程和主线程一起运行,主线程只打印10次,而后台线程打印1000次,当主线程打印完成以后,后台线程就不运行了,所以没有打印完1000次就退出。

你可能感兴趣的:(jvm,thread,360)