让线程实现暂停,重新启动,停止

public class BaseThread extends Thread{
        private final static Logger logger = Logger.getLogger(BaseThread.class);

        /**
         * isDead:是否杀死线程
         */
        private boolean isDead = false;
        
        /**
         * isStop:是否停止
         */
        private boolean isStop = false;
        
        /**
         * isRun:是否已开始执行
         */
        private boolean isRun = false;
        
        /**
         * isWait:是否处于等待
         */
        private boolean isSleep = false;
        
        public BaseThread() {
                super();
                this.setDaemon(false);//设置为非守护线程
                logger.info("线程:["+this.getId()+"] 被创建");
        }
        
        public BaseThread(String threadName) {
                super(threadName);
                this.setDaemon(false);//设置为非守护线程
                logger.info("线程:["+threadName+"-"+this.getId()+"] 被创建");
        }

        /**
         *<p>Title: run</p>
         *<p>Description:JDK线程类自带方法</p>
         * @param @return 设定文件
         * @return  boolean 返回类型
         * @throws
        */
        public void run() {
                this.isRun = true;
                while(!isDead){
                        while(true){
                                if(!isStop){
                                        if(preConditions())execute();
                                }else{
                                        break;
                                }
                                sleep(256);//缓解CPU压力,即唤醒线程需要至多256ms
                        }
                }
                isRun = false;
                logger.info("线程:[" + this.getName() +"-"+this.getId()+ "] 消亡");
        }
        
        /**
         *<p>Title: preConditions</p>
         *<p>Description:执行体前置条件</p>
         * @param @return 设定文件
         * @return  boolean 返回类型
         * @throws
        */
        protected boolean preConditions(){
                return true;
        }
        
        /**
         *<p>Title: execute</p>
         *<p>Description:线程执行体</p>
         * @param  设定文件
         * @return  void 返回类型
         * @throws
        */
        protected void execute(){
                        
        }

        /**
         *<p>Title: kill</p>
         *<p>Description:结束线程</p>
         * @param  设定文件
         * @return  void 返回类型
         * @throws
        */
        public void kill(){
                this.isStop = true;
                this.isDead = true;
                this.isRun = false;
                logger.info("线程:["+this.getName()+"-"+this.getId()+"] 被终止");
        }
        
        /**
         *<p>Title: halt</p>
         *<p>Description:暂停进程,非休眠</p>
         * @param  设定文件
         * @return  void 返回类型
         * @throws
        */
        public void halt(){
                this.isStop = true;
                logger.info("线程:["+this.getName()+"-"+this.getId()+"] 被暂停");
        }
        
        /**
         *<p>Title: reStart</p>
         *<p>Description:重新执行线程</p>
         * @param  设定文件
         * @return  void 返回类型
         * @throws
        */
        public void reStart(){
                this.isStop = false;
                logger.info("线程:["+this.getName()+"-"+this.getId()+"] 被重新执行");
        }

        /**
         *<p>Title: isRun</p>
         *<p>Description:是否处于执行态</p>
         * @param @return 设定文件
         * @return  boolean 返回类型
         * @throws
        */
        public boolean isRun() {
                return isRun;
        }

        /**
         *<p>Title: isSleep</p>
         *<p>Description:是否处于休眠态</p>
         * @param @return 设定文件
         * @return  boolean 返回类型
         * @throws
        */
        public boolean isSleep() {
                return isSleep;
        }
        
        public boolean isDead(){
                return isDead;
        }
        
        /**
         *<p>Title: sleep</p>
         *<p>Description:休眠线程</p>
         * @param @param millis
         * @param @throws InterruptedException 设定文件
         * @return  void 返回类型
         * @throws
        */
        public void sleep(int millis){
                isSleep = true;
                try {
                        Thread.sleep(millis);
                        this.sleepTime += millis;
                        if(notifyPreConditions())notifyObs();
                } catch (InterruptedException e) {
                        e.printStackTrace();
                }
                isSleep = false;
        }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值