JAVA Executor实现并发线程_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 1737 | 回复: 0   主题: JAVA Executor实现并发线程        下一篇 
songjian
注册用户
等级:上尉
经验:711
发帖:60
精华:0
注册:2012-11-8
状态:离线
发送短消息息给songjian 加好友    发送短消息息给songjian 发消息
发表于: IP:您无权察看 2015-12-11 15:33:49 | [全部帖] [楼主帖] 楼主

通过Executor来管理线程,包括了启动线程和关闭线程等。通过java.util.concurrent.ExecutorService


对象来启动线程并执行任务,因为java.util.concurrent.ExecutorService的execute方法参数类型是Runnable类的实例,比采用Thread的start()来启动线程会

比较好。

  

1.Executor简单的实现线程:

 //线程数  
xecutorService threadPool=Executors.newFixedThreadPool(3);  
 for (int i = 0; i < 20; i++) {  
  Runnable runnable=new Runnable() {  
    @Override  
    public void run()   
    {  
         System.out.println("启动线程=="+Thread.currentThread().getName());  
    }  
  };  
  //放进任务  
       threadPool.execute(runnable);  
 }  
 关闭启动线程  
threadPool.shutdown();


说明:

(1)通过java.util.concurrent.Executors实现线池程管理工具,有3种类型 

     ExecutorService executorService = Executors.newCachedThreadPool();

     ExecutorService executorService = Executors.newFixedThreadPool(threadPoolNum);

     ExecutorService executorService = Executors.newSingleThreadExecutor();

(2)java.util.concurrent.ExecutorService的execute把任务添加到线程池里执行。

(3)java.util.concurrent.ExecutorService 的shutdown();关闭已经启动的线程。


2.我们要让线程执行针对的业务,好比如有好几个通道,一个线程对应一个通道,来发送相应的信息。


先把线程和通道对应起来,放在容器里,代码实现:

threadPool=Executors.newFixedThreadPool(threadPoolNum, new ThreadFactory(){  
    @Override  
    public Thread newThread(Runnable r)   
    {  
        Thread thread=new Thread(r);  
        Channel channel = connectManage.createChannel();
        //新建一个通道  
        if(channel!=null)  
            channelManager.put(thread, channel);
            //<span style="color: rgb(85, 85, 85); 
            font-family: 'Times New Roman';
            font-size:18px; line-height: 28px;
             ">一个线程对应一个通道</span>  
        return thread;  
    }             
});

执行线程并发送消息到相应的通道里。代码实现如下:

Runnable runnable=new Runnable() {  
    @Override  
    public void run()   
    {  
        Thread thread=Thread.currentThread();//获取当前线程  
        Channel channel=channelManager.get(thread);//取出线程对应的通道  
        try {  
            channel.basicPublish(msg.getExchange(),msg.getRouteKey(),MessageProperties.  PERSISTENT_TEXT_PLAIN,msg.getSerialBytes());
            //发送消息  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
};  
threadPool.execute(runnable);//添加任务

  

3.我们可以获取线程执行有结果是TRUE还是FALSE。具体实现:

 public class Task implements CompilationTask{  
    /** 
     * 设置处理器(用于注释处理) 
     */  
    @Override  
    public void setProcessors(Iterable<? extends Processor> processors) {  
        //      
    }  
    /** 
     * 设置格式化诊断和其他本地化数据时要应用的语言环境 
     */  
    @Override  
    public void setLocale(Locale locale) {  
    }  
    /** 
     * 执行此编译任务。编译只能被执行一次 
     */  
    @Override  
    public Boolean call() {  
        System.out.println(Thread.currentThread().getName());  
        return true;  
    }


Future<Boolean> future=threadPool.submit(new Task());  
System.out.println(future.get());  
// 关闭启动线程  
 threadPool.shutdown();

 

说明:

java.util.concurrent.ExecutorService的submit,参数类型是Runnable类型,实现具体的任务,然后这个任务执行是否成功,我们可以通过返回值进行判断,

方便我们管理。


总结:

JAVA  Executor实现并发线程,比采用Thread的start()来启动线程会比较好。






                                                                                                                          --转自



赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论