java thread worker thread 小例

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解锁同时发生时,才会到执行那个内容

阅读更多

HTML5之Worker Thread

12-06

传统网页中嵌入的JSP语言都是执行在UI的上下文中,一般来说这种JSP处理的都是简单逻辑,没啥问题,随着Web复杂度越来高,JSP能处理的事情也越来越复杂,如果JSP计算量比较大的话,这就会导致UI页面假死。HTML5中的WorkerThread可以缓解这个问题,Worker Thread通过UI thread创建一个新的线程来进行计算,消息和结果通过Web Message来进行通讯,由于这种异步性,UI便可以较为流畅地响应新到来的事件。rn调用过程如下:rn1.新的工作线程执行体放在一个单独的JS文件中,比如worker.jsrn01.self.onmessage = function(event) 02. //do the real compute ---begin rn03. // 04. //do the real compute ---end rn05. //post message back to UI thread 06. self.postMessage(XXXX); rn07.; rn2.在UI的执行线程中生成一个worker对象,worker对象的构造函数参数就是worker.jsrn01.var worker = new Worker("worker.js"); rn3.下面就可以发送消息让worker干活了rn01.worker.postMessage("perise"); rn4.活干完了给UI发送消息,也就是第一步中的rn01.self.postMessage(XXXX); rn5.UI进行响应rn01.worker.onmessage = function(event) 02. //update UI here rn03. ;rnNotes: rn*在Chrome中调试的时候会出现SECURITY_ERR: DOM Exception 18,传说是Webkit的bug,没有细究,可以通过搭建一个本地的HTTP服务器来解决,把HTML文件放到本地服务器上就可以绕开这个安全问题了,我使用的是WAMP。rn*也许是为了控制Worker Thread的复杂度,Chrome的Worker Thread中不能再生成新的worker thread,其实这个很容易绕开,发送一个消息到UI中,让UI生成新的Worker Thread就好,Firefox可以支持subworker,但Chrome是可以支持sharework,这种shareworker可以共享JS文件里面的全局数据。rnrnrnrnrn文章转自: http://software.intel.com/zh-cn/blogs/2011/11/22/html5worker-thread/rnrn

没有更多推荐了,返回首页