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;
        }
    }
  • 相关阅读:
    模拟手机售电影票
    flex布局
    如何制作快速加载的HTML页面
    css布局列表,自适应
    iOS项目中常见定时器
    iOS中TableView的分割线顶格样式的实现
    iOS本地化项目上传到gitHub
    iOS初学者易懵逼的Timer延时
    iOS同种界面moda和push方式切换
    iOS之push和modal大不同
  • 原文地址:https://www.cnblogs.com/cxmsky/p/2860996.html
Copyright © 2011-2022 走看看