`
baobeituping
  • 浏览: 1068803 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

线程的后台线程

阅读更多
后台线程是在后台运行的,他的任务是为其他的线程提供服务,也成为“守护线程”。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次就退出。
分享到:
评论

相关推荐

    前台线程后台线程区别实验

    在实验"前台线程后台线程区别"中,可能会涉及创建和管理这两种类型的线程,观察它们如何影响应用程序的关闭行为,以及如何通过代码控制线程的切换。可能的步骤包括: 1. 创建一个前台线程和一个后台线程,让它们...

    Pker多线程后台急速扫描器

    《Pker多线程后台急速扫描器:深入理解与应用》 Pker多线程后台急速扫描器是一款针对网站安全的高效工具,主要用于检测和扫描潜在的后台漏洞和敏感地址。在网络安全日益重要的今天,此类工具的应用对于保障系统安全...

    多线程后台扫描工具

    在IT领域,多线程后台扫描工具是一种常用于网络安全与系统审计的重要软件。它通过同时执行多个任务,即多线程技术,来提高扫描效率,快速遍历目标系统的潜在漏洞和弱点。这样的工具通常用于安全研究人员、渗透测试者...

    android多线程后台下载

    android多线程后台下载示例程序,android多线程后台下载示例程序,android多线程后台下载示例程序,android多线程后台下载示例程序,android多线程后台下载示例程序,android多线程后台下载示例程序

    Pker多线程后台极速扫描工具.zip

    《Pker多线程后台极速扫描工具:提升网络安全检测效率》 在信息技术高速发展的今天,网络安全成为了企业乃至个人用户关注的焦点。面对日益复杂的网络环境,有效的安全扫描工具显得尤为重要。Pker多线程后台极速扫描...

    Pker多线程后台极速扫描2.01修正版

    Pker多线程后台极速扫描2.01修正版 速度很快的扫描工具 可以扫描后台地址,网站目录,数据库,上传地址

    线程后台的作用演示

    本文将深入探讨“线程后台的作用演示”,并基于MSDN(Microsoft Developer Network)的官方解释来解析多线程以及后台线程设置的区别。 首先,我们需要明白什么是线程。线程是程序执行的流,一个进程中可以包含多个...

    Pker多线程后台极速扫描器.zip

    低积分下载,恶心一下那些搬运资源还要高积分的。声明:资源为转载资源,只用于技术交流,请勿使用在违法道路上。

    Pker多线程后台极速扫描工具2.01修正版

    Pker多线程后台极速扫描工具2.01修正版

    COCOS2DX实现线程后台运作与推送

    本主题聚焦于如何利用COCOS2DX实现线程后台运作以及推送通知功能,特别是在用户按下HOME键后进入后台模式时。这涉及到iOS的多任务处理和推送服务(例如JGPush)的集成。 首先,让我们深入理解iOS的后台运行模式。...

    超强多线程后台扫描器.zip

    超强多线程后台扫描器

    WinForm后台线程与UI线程通讯

    在Windows Forms(WinForm)应用程序中,经常遇到需要在后台线程执行耗时操作,如数据处理、网络请求等,而这些操作的结果需要更新到用户界面(UI)上,例如显示进度条。由于.NET Framework的安全机制,直接在非UI...

    Pker多线程后台极速扫描工具2[1].01修正版.

    标题中的"Pker多线程后台极速扫描工具2[1].01修正版"是一个特定的软件版本,这表明我们正在讨论一个用于网络安全扫描的工具,它具有多线程功能,这意味着它可以同时处理多个任务,提高扫描速度。"修正版"意味着这个...

    多线程--下载---后台运行

    在实际开发中,多线程后台下载还涉及到其他一些关键点: 1. **线程池管理**:为了避免大量线程的创建和销毁带来的性能开销,可以使用线程池(ThreadPoolExecutor)来管理线程,合理分配系统资源。 2. **断点续传**...

    多线程后台查询数据

    多线程后台查询数据

    WPF后台线程更新UI

    在Windows Presentation Foundation(WPF)开发中,UI(用户界面)通常由主线程管理,而后台线程用于执行耗时的任务。然而,由于GUI组件不是线程安全的,直接在后台线程修改UI元素可能会导致应用程序崩溃或出现不可...

    多线程网站后台扫描工具1.3.rar

    【多线程网站后台扫描工具】是一种常见的网络安全与渗透测试技术的应用,主要用于发现网站的潜在安全漏洞和弱点。在IT领域,多线程是提高程序执行效率的重要手段,尤其是在网络扫描这种需要处理大量并发请求的任务中...

    多线程网站后台扫描工具

    可扫描网站后台 一个多线程的后台管理扫描器 两种扫描模式:线程扫描 普通扫描

    C#线程参考手册,让你的编程更进一层

    后台线程不会阻止应用程序的退出,而主线程和其他前台线程会。`IsBackground`属性可以设置线程为后台线程。 总之,理解和掌握C#线程是编写高效并发程序的关键。通过合理使用线程同步、异步编程、线程池以及各种同步...

    多线程网站后台扫描工具1.3

    多线程网站后台扫描工具1.3 1.两种扫描模式:线程扫描 普通扫描 2.程序会检测robots.txt存在提示查看 3.可自行外挂后台地址字典 4.可添加排除关键字 1.2更新:2008.04.22 修正部分目录无法正常扫描出来 1.3更新:...

Global site tag (gtag.js) - Google Analytics