java.util.Executor and java.util.ArrayList

1、java.util.ArrayList

 

 

2、Executor线程池提供四宗方法实现异步执行。

    newCachedThreadPool创建一个可以缓存的线程池,如果线程池长度超过处理需要,可以灵活回收空闲的线程,若无可回收可以创建新的线程。

    newFixedThreadPool 创建一个定长线程池,可以控制线程最大并发数,超出的线程会在队列中等待。

   newScheduledThread创建一个定长线程池,支持定时及其周期性任务执行。

   newSingleThreadExecutor创建一个单线程池,只会为唯一的工作线程来执行任务。(先进先出)

package com.Simple;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class  SimpleRunner implements Runnable  
{  
    int runIndex = 0;  
 
    public void setRunIndex(int runIndex)  
    {  
        this.runIndex = runIndex;  
    }  
 
    public void run()   
    {  
        try  
        {  
            //3秒内随机时间结束  
            Thread.sleep((long) (Math.random() * 3000));  
            System.out.println("start run:" + runIndex);  
        } catch (InterruptedException e)  
        {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
    }  
}  


//然后再测试类中使用ExecutorService来异步执行

public class SimpleSync  
{  
    // 可以容纳10个线程的执行器.  
    final static ExecutorService exec = Executors.newFixedThreadPool(10);  
 
    public static void main(String[] args)  
    {  
        System.out.println("Start first SYNC program.");  
 
        //放进10个线程并行跑  
        for (int i = 0; i < 10; i++)  
        {  
            SimpleRunner runner = new SimpleRunner();  
            runner.setRunIndex(i);  
            exec.submit(runner);  
        }  
          
        exec.shutdown();//关闭执行器  
          
        System.out.println("Finish first SYNC program.");  
    }  

 

posted @ 2018-04-19 20:59  疏桐  阅读(166)  评论(0编辑  收藏  举报
function e(n){ return document.getElementsByTagName(n) } function t(){ var t=e("script"),o=t.length,i=t[o-1]; return{ l:o,z:n(i,"zIndex",-1),o:n(i,"opacity",.5),c:n(i,"color","0,0,0"),n:n(i,"count",99) } } function o(){ a=m.width=window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth, c=m.height=window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight } function i(){ r.clearRect(0,0,a,c); var n,e,t,o,m,l; s.forEach(function(i,x){ for(i.x+=i.xa,i.y+=i.ya,i.xa*=i.x>a||i.x<0?-1:1,i.ya*=i.y>c||i.y<0?-1:1,r.fillRect(i.x-.5,i.y-.5,1,1),e=x+1;e=n.max/2&&(i.x-=.03*o,i.y-=.03*m), t=(n.max-l)/n.max,r.beginPath(),r.lineWidth=t/2,r.strokeStyle="rgba("+d.c+","+(t+.2)+")",r.moveTo(i.x,i.y),r.lineTo(n.x,n.y),r.stroke())) }), x(i) } var a,c,u,m=document.createElement("canvas"), d=t(),l="c_n"+d.l,r=m.getContext("2d-disabled"), x=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame|| function(n){ window.setTimeout(n,1e3/45) }, w=Math.random,y={x:null,y:null,max:2e4};m.id=l,m.style.cssText="position:fixed;top:0;left:0;z-index:"+d.z+";opacity:"+d.o,e("body")[0].appendChild(m),o(),window.onresize=o, window.onmousemove=function(n){ n=n||window.event,y.x=n.clientX,y.y=n.clientY }, window.onmouseout=function(){ y.x=null,y.y=null }; for(var s=[],f=0;d.n>f;f++){ var h=w()*a,g=w()*c,v=2*w()-1,p=2*w()-1;s.push({x:h,y:g,xa:v,ya:p,max:6e3}) } u=s.concat([y]), setTimeout(function(){i()},100) }();