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;
        }
    }
  • 相关阅读:
    抽奖代码
    org.hibernate.AssertionFailure: null id in com.you.model.User entry (don't flush the Session after a
    Cannot add or update a child row: a foreign key constraint fails
    SyntaxError:identifier starts immediately after numeric literal
    too much recursion
    微信处理红包
    minerd
    minerd
    kill常用
    阿里云centos 6安装Nginx+PHP+MySQL
  • 原文地址:https://www.cnblogs.com/cxmsky/p/2860996.html
Copyright © 2011-2022 走看看