setDaemon(), 设置一个线程为守护线程, 该线程不会单独执行, 当其他非守护线程都执行结束后, 自动退出
- Thread t1 = new Thread() {
- public void run() {
- for(int i = 0; i < 50; i++) {
- System.out.println(getName() + "...aaaaaaaaaaaaaaaaaaaaaa");
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- };
- Thread t2 = new Thread() {
- public void run() {
- for(int i = 0; i < 5; i++) {
- System.out.println(getName() + "...bb");
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- };
- t1.setDaemon(true); //将t1设置为守护线程
- t1.start();
- t2.start();
复制代码