java thread worker thread 小例

    技术2022-05-11  60

    import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;

    public class WorkThread extends Thread {

        static Log log = LogFactory.getLog(WorkThread.class);        private Runnable work;        private boolean exitFlag = false;  //是否应该退出        private ThreadPool tp;        private boolean isWorkFlag = false;   //是否在"工作",即是否在运行executeContent过程中        private int threadId;    //ID        public WorkThread( int threadId ) {        this.threadId = threadId;        super.setName( "Thread-" + threadId );        start();    }

        public WorkThread( int threadId, ThreadPool tp ) {        this.threadId = threadId;        super.setName( "Thread-" + threadId );        this.tp = tp;        start();    }        public synchronized void run() {                log.info(this.getName() + " run begin, This worker thread will accept work now.");                while( true ) {            try {                this.wait();            } catch(Exception e) {            }            log.debug("Not wait be notified!!!");            if( isWorkFlag ) {                if( work != null) {                    work.run();                }                isWorkFlag = false;            }            if( exitFlag ) {                break;            }        }        log.info(this.getName() + " run end. This worker thread will not accept work now.");            }        public synchronized void doAWork(Runnable work) {        log.debug("###########begin to do work");        this.isWorkFlag = true;        this.work = work;        this.notify();    }   

        /**     * 工作线程退出     *     */    public synchronized void exit() {        log.info( this.getName() + " receive exit command, will exit..." );               this.exitFlag = true;        this.notify();    }

        public Runnable getWork() {        return work;    }

     

        public void setWork(Runnable executeContent) {        this.work = executeContent;    }

        public boolean isExitFlag() {        return exitFlag;    }

        public void setExitFlag(boolean exitFlag) {        this.exitFlag = exitFlag;    }

        public boolean isWorkFlag() {        return isWorkFlag;    }

        public void setWorkFlag(boolean isWorkFlag) {        this.isWorkFlag = isWorkFlag;    }

        public int getThreadId() {        return threadId;    }

        public void setThreadId(int threadId) {        this.threadId = threadId;    }

        public ThreadPool getTp() {        return tp;    }

        public void setTp(ThreadPool tp) {        this.tp = tp;    }            public static void main( String[] args ) {        Thread t1 = new Thread() {            public void run() {                int count = 0;                while(true) {                    System.out.println(this.getName() + " running......");                    try {                        Thread.sleep(100);                    } catch(Exception e) {                                        }                    ++count;                    if(count >= 10) {                        break;                    }                }            }        };                        Thread t2 = new Thread() {            public void run() {                int count = 0;                while(true) {

                        System.out.println(this.getName() + " running......");                    try {                        Thread.sleep(100);                    } catch(Exception e) {                                        }                    ++count;                    if(count >= 10) {                        break;                    }                                    }            }        };                Thread t3 = new Thread() {            public void run() {                int count = 0;                while(true) {

                        System.out.println(this.getName() + " running......");                    try {                        Thread.sleep(100);                    } catch(Exception e) {                                        }                    ++count;                    if(count >= 10) {                        break;                    }                                          }            }        };                                WorkThread wt = new WorkThread(999);        try{            //Thread.sleep( 1000 );            } catch(Exception e) {                    }                wt.doAWork( t1 );        wt.doAWork( t2 );        wt.doAWork( t3 );        wt.doAWork( t3 );        wt.doAWork( t3 );        wt.doAWork( t3 );        wt.doAWork( t2 );        wt.exit();            }}

    两个问题: 1. WorkThread wt = new WorkThread(999);    new了之后如果不sleep一点时间,马上就调用doAWork 方法,则可能不执行,因为 wt自身还没有start好的   2. wt.doAWork( t1 ); ....wt.doAWork( t2 );  连续调用这些语句,从第2个开始阻塞,等到第1个work执行完毕, 第2个notify的时候,wt没有马上从sleep状态跳出,也不会去执行第2个,第3个....的内容, 跳过去了. 直到主程序的 调用到的一句wt.doAWork( tn )和WorkThread的wait解锁同时发生时,才会到执行那个内容

    最新回复(0)