新的起点 新的开始

快乐生活 !

学习实践 JDK5 concurrent 并行包之executor(二)

这是介绍使用JDK 并行包之executor 第二部分,这部分介绍是如何创建一个可以调度的线程池。具体参考代码中的注释。
/*
         * delay 4 seconds and run thread every 2 seconds.
         
*/
        
//ses.scheduleAtFixedRate(GetRunnable(), 4, 2, TimeUnit.SECONDS);
        
        
/*
         * delay 4 seconds and run thread delay 1 second.
         
*/
        ses.scheduleWithFixedDelay(GetRunnable(), 
21, TimeUnit.SECONDS);
        
        
/*
上述代码是关键。




package net.vincent.study.executor;

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

    
/**
     * This is example of using ScheduleExecutorService.
     * 
@author wma
     
*/

public class Part2 {

    
/**
     * Create scheduleThreadPool
     * 
@param numberOfTHread in threadPool.
     * 
@return ScheduleThreadPool
     
*/
    
public static ScheduledExecutorService getScheduleThreadPool(int numberOfThread){
        
if(numberOfThread <=0)
            
return null;
        ScheduledExecutorService  scheduleThreadPool 
= Executors.newScheduledThreadPool(numberOfThread) ;
        
return scheduleThreadPool;
    }
    
/**
     * 
     * Create Runnable 
     
*/
    
public static Runnable GetRunnable(){
        
final Runnable runnable = new Runnable() {
              
int count = 0;
              
public void run() {
                System.out.println(
new Date() + " run " + (++count));
              }
            };
        
return runnable;
        
    }
    
    
    
public static void main(String[] args) {
        ScheduledExecutorService ses 
= getScheduleThreadPool(4);
        System.out.println(
new Date());
        
/*
         * delay 4 seconds and run thread every 2 seconds.
         
*/
        
//ses.scheduleAtFixedRate(GetRunnable(), 4, 2, TimeUnit.SECONDS);
        
        
/*
         * delay 4 seconds and run thread delay 1 second.
         
*/
        ses.scheduleWithFixedDelay(GetRunnable(), 
21, TimeUnit.SECONDS);
        
        
/*
         * ses should be shutdown.
         
*/
        
//ses.shutdown();

    }

}

posted on 2007-09-26 17:16 advincenting 阅读(720) 评论(1)  编辑  收藏

评论

# re: 学习实践 JDK5 concurrent 并行包之executor(二) 2007-09-27 19:19 千里冰封

不会吧,就这样帖点代码,也解释一下吧  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

公告

Locations of visitors to this page

导航

<2007年9月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

统计

常用链接

留言簿(13)

随笔分类(71)

随笔档案(179)

文章档案(13)

新闻分类

IT人的英语学习网站

JAVA站点

优秀个人博客链接

官网学习站点

生活工作站点

最新随笔

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜