ThreadWait.java 2.8 KB
Newer Older
yanzg's avatar
yanzg committed
1 2 3
package com.yanzuoguang.util.thread;


yanzg's avatar
yanzg committed
4
import com.yanzuoguang.util.extend.ConfigBase;
yanzg's avatar
yanzg committed
5 6 7 8 9 10 11 12
import com.yanzuoguang.util.log.Log;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 等待某个任务完成后才能继续往下执行
yanzg's avatar
yanzg committed
13
 * @author 颜佐光
yanzg's avatar
yanzg committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
 */
public class ThreadWait {

    /**
     * 需要判断的任务
     */
    private ThreadWaitExecute execute;

    /**
     * 取消定时任务
     */
    private Timer timer;

    /**
     * 时间
     */
    private volatile long time;

    /**
     * 构造函数
     *
     * @param execute 需要判断的对象
     * @description 构造函数
     */
    public ThreadWait(ThreadWaitExecute execute) {
        this.execute = execute;
    }

    /**
     * 等待任务完成
     *
     * @return void
     * @description
     */
    public synchronized void waitFinally() {
        if (this.execute == null) {
            return;
        }
        if (!this.execute.isFinally()) {
            this.activePrint();
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            this.stopPrint();
        }
yanzg's avatar
yanzg committed
61
        if (ConfigBase.PRINT_THREAD) {
yanzg's avatar
yanzg committed
62 63 64 65 66 67 68 69 70 71 72
            Log.info(ThreadWait.class, "客户端结束时间");
        }
    }

    /**
     * 当完成一任务时,用该函数进行通知,用于进行下一次判断
     *
     * @return void
     * @description
     */
    public void nexted() {
yanzg's avatar
yanzg committed
73
        this.time = System.currentTimeMillis();
yanzg's avatar
yanzg committed
74 75 76 77 78 79 80 81
    }

    /**
     * 结束运行
     */
    public synchronized void finish() {
        if (this.execute.isFinally()) {
            this.notify();
yanzg's avatar
yanzg committed
82
            if (ConfigBase.PRINT_THREAD) {
yanzg's avatar
yanzg committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
                Log.info(ThreadWait.class, "客户端结束时间");
            }
        }
    }

    /**
     * 启动定时任务,用于定时打印状态
     *
     * @param
     * @return void
     * @description
     */
    private synchronized void activePrint() {
        if (this.execute == null) {
            return;
        }
        this.stopPrint();
        if (!this.execute.isFinally()) {
yanzg's avatar
yanzg committed
101
            this.time = System.currentTimeMillis();
yanzg's avatar
yanzg committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
            this.timer = new Timer();
            this.timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    Date now = new Date();
                    if (now.getTime() - time > execute.printTimeout()) {
                        execute.printStatus();
                    }
                }
            }, this.execute.printTimeout());
        }
    }

    /**
     * 停止打印状态
     *
     * @param
     * @return void
     * @description
     */
    private synchronized void stopPrint() {
        if (this.timer != null) {
            this.timer.cancel();
        }
        this.timer = null;
    }

}