zoukankan      html  css  js  c++  java
  • Java基础多线程之后台守护线程,setDaemon(true)

    class DaemonThreadDemo
    {
        public static void main(String[] args)
        {
            StopThread stopThread = new StopThread();
        
            Thread t1 = new Thread(stopThread,"t1");
            Thread t2 = new Thread(stopThread,"t2");
            
            t1.setDaemon(true);
            t2.setDaemon(true);
            
            t1.start();
            t2.start();
            
            int num=0;
            while(true)
            {
                if(num++ ==60)
                {            
                    stopThread.changeFlag();
                    break;
                }
                
                System.out.println(Thread.currentThread().getName()+ " runing.... "+ num);
            }
            
            System.out.println("end");
        }
    }

    class StopThread implements Runnable
    {
        private boolean flag = true;
        public synchronized void run()
        {        
            while(this.flag)
            {
                try
                {
                    wait();
                }
                catch(InterruptedException ex)
                {
                    System.out.println(Thread.currentThread().getName() + ": Exception");
                    this.changeFlag();
                }
                System.out.println(Thread.currentThread().getName() + ": runing...");
            }
        }
        
        public void changeFlag()
        {
            this.flag = false;
        }
    }
  • 相关阅读:
    Angular2.0 基础:双向数据绑定 [(ngModel)]
    Angular2.0 基础: 环境搭建
    将已编写的静态的网页发布到github上
    kndo grid:通过checkbox 实现多选和全选
    Kendo Grid:将Edit button 移到grid view 得顶部
    溢出文本显示省略号处理
    空MVC项目找不到System.Web.Optimization的处理办法
    cannot find module 'xml2js'
    jquery mobile RedirectToAction url地址不更新
    soapUI 时间格式
  • 原文地址:https://www.cnblogs.com/cxmsky/p/2860996.html
Copyright © 2011-2022 走看看