<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Concurrency - why use & many faces

    As you shall see, the real problem with concurrency occurs when tasks that are executing in parallel begin to interfere with each other.
    If you ignore it, you're likely be get bitten.

    The many faces of concurrency:
    The problems that you solve with concurrency can be roughly classified as "speed" and "design manageability."
    1. Faster execution -If you want a program to run faster, break it into pieces and run each piece on a separate processor.
    2. Utilize multiple-precessor system

    To define a task, simply implement Runnable and write a run( ) method to make the task do your bidding.
    /**
     * Display the countdown before liftoff.
     * Demonstration of the Runnable interface
     * 
    @author WPeng
     *
     * 
    @since 2012-11-19
     
    */
    public class LiftOff implements Runnable{
        
    protected int countDown = 10//Default
        private static int taskCount = 0;
        
    private final int id = taskCount++;
        
        
    public LiftOff(){}
        
        
    public LiftOff(int countDown){
            
    this.countDown = countDown;
        }
        
        
    public String status(){
            
    return "#" + id + "(" + (countDown > 0 ? countDown : "LiftOff"+ ") ,";
        }

        @Override
        
    public void run() {
            
    while(countDown-- >0){
                System.out.println(status());
                
    //I’ve done the important parts of my cycle 
                
    //and this would be a good time to switch to another task for a while.
                Thread.yield();
            }
        }
        
        
    public static void main(String[] args){
            LiftOff launch 
    = new LiftOff();
            launch.run();
        }
    }
    /**
    #0(9) ,
    #0(8) ,
    #0(7) ,
    #0(6) ,
    #0(5) ,
    #0(4) ,
    #0(3) ,
    #0(2) ,
    #0(1) ,
    #0(LiftOff) ,
     
    */

     




    A Thread constructor only needs a Runnable object
    Calling a Thread object’s start( ) will perform the necessary initialization for the thread and then call that Runnable’s run( ) method to start the task in the new thread.

    /**
     * The most basic use of Thread class
     * 
    @author WPeng
     *
     * 
    @since 2012-11-20
     
    */
    public class BasicThreads {

        
    public static void main(String[] args) {
            Thread t 
    = new Thread(new LiftOff());
            t.start();
            System.out.println(
    "Waiting for LiftOff");
        }
    }
    /**
     *     Waiting for LiftOff
     *    #0(9) ,#0(8) ,#0(7) ,#0(6) ,#0(5) ,#0(4) ,#0(3) ,#0(2) ,#0(1) ,#0(LiftOff) ,
     * 
    */

     



     

    Executors are the preferred method for starting tasks in Java SE5/6.
    We can use an Executor instead of explicitly creating Thread objects.
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;

    public class CashedThreadPool {

        
    public static void main(String[] args) {
            ExecutorService exec 
    = Executors.newCachedThreadPool();
            
    for(int i=0; i<5; i++)
                exec.execute(
    new LiftOff());
            exec.shutdown();
        }
    }
    Very often, a single Executor can be used to create and manage all the tasks in your system.
    The current thread (in this case, the one driving main( )) will continue to run all tasks submitted before shutdown( ) was called. The program will exit as soon as all the tasks in the Executor finish.


     


     

    Note that in any of the thread pools, existing threads are automatically reused when possible.


    A Runnable is a separate task that performs work, but it doesn’t return a value. If you want the task to produce a value when it’s done, you can implement the Callable interface rather than the Runnable interface.
    class TaskWithResult implements Callable<String>{
        
    private int id;
        
    public TaskWithResult(int id){
            
    this.id = id;
        }
        
        @Override
        
    public String call() {
            
    return "result of TaskWithResult " + id;
        }
    }

    public class CallableDemo  {

        
    public static void main(String[] args) {
            ExecutorService exec 
    = Executors.newCachedThreadPool();
            ArrayList
    <Future<String>> results = new ArrayList<Future<String>>();
            
    for(int i=0; i<10; i++)
                
    //with a type parameter representing the return value from the method call( ) 
                
    //(instead of run( )), and must be invoked using an ExecutorService submit( ) method.
                results.add(exec.submit(new TaskWithResult(i)));
            
    for(Future<String> fs : results){
                
    try {
                    
    // get() blocks until completion:
                    System.out.println(fs.get());
                } 
    catch (Exception e) {
                    
    // TODO: handle exception
                } finally{
                    exec.shutdown();
                }
            }
        }
    }

     



    posted on 2012-11-16 15:43 鹽城小土包 閱讀(165) 評論(0)  編輯  收藏 所屬分類: J2EE

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導(dǎo)航

    統(tǒng)計(jì)

    常用鏈接

    留言簿

    隨筆檔案(14)

    文章分類(18)

    文章檔案(18)

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 日韩免费a级在线观看| 91精品导航在线网址免费| 国产视频精品免费| 一级黄色免费大片| 免费看一级做a爰片久久| 国产精品亚洲精品久久精品| 成人毛片免费网站| 亚洲AV无码AV男人的天堂不卡| 大学生高清一级毛片免费| 亚洲午夜无码久久| 四虎国产精品免费视| 曰批免费视频播放在线看片二| 亚洲 综合 国产 欧洲 丝袜 | 成人无遮挡裸免费视频在线观看 | 亚洲AV成人一区二区三区观看 | 亚洲乱码卡三乱码新区| 国产福利在线免费| 亚洲AV成人无码网天堂| 免费成人黄色大片| 国产午夜成人免费看片无遮挡 | 国产AV无码专区亚洲AV男同 | 国产精品亚洲二区在线观看| 中文无码日韩欧免费视频| 久久久久无码精品亚洲日韩 | 免费人成在线观看69式小视频| 国产免费av片在线播放| 一区二区三区免费视频观看| 亚洲AV无码码潮喷在线观看| 4455永久在线观免费看| 欧美亚洲国产SUV| 亚洲无线码在线一区观看| 亚洲一区免费观看| 国产精品亚洲专区无码牛牛| 亚洲欧洲∨国产一区二区三区| 国产1000部成人免费视频| 精品国产_亚洲人成在线| 精品国产_亚洲人成在线高清| 国产精品久久永久免费| 无码 免费 国产在线观看91| 久久久亚洲欧洲日产国码二区 | 国产成人亚洲综合|