<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 鹽城小土包 閱讀(169) 評論(0)  編輯  收藏 所屬分類: J2EE

    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    導(dǎo)航

    統(tǒng)計

    常用鏈接

    留言簿

    隨筆檔案(14)

    文章分類(18)

    文章檔案(18)

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 久久精品国产亚洲AV未满十八| 亚洲国产精品综合一区在线| 美女一级毛片免费观看| 成人毛片18女人毛片免费视频未| 亚洲综合激情另类小说区| 131美女爱做免费毛片| 337p日本欧洲亚洲大胆艺术| 5555在线播放免费播放| 亚洲成无码人在线观看| 久久久久久久免费视频| 亚洲精品无码久久久久秋霞 | yellow视频免费看| 亚洲精品一级无码鲁丝片| 三根一起会坏掉的好痛免费三级全黄的视频在线观看 | 国产精品玖玖美女张开腿让男人桶爽免费看 | 成年大片免费高清在线看黄| 亚洲国产精品尤物yw在线| 丰满人妻一区二区三区免费视频| 狠狠综合久久综合88亚洲| 97无码人妻福利免费公开在线视频 | 免费精品国产自产拍观看| 色多多免费视频观看区一区| 国产成人精品曰本亚洲79ren| 精品国产呦系列在线观看免费| 亚洲av永久无码精品古装片 | 亚洲中文字幕久久精品无码2021| 在线天堂免费观看.WWW| 国产成人亚洲精品播放器下载| 亚洲精品成人在线| 99re在线免费视频| 人人爽人人爽人人片A免费| 国产偷国产偷亚洲高清日韩 | 亚洲午夜日韩高清一区| 久久久精品午夜免费不卡| 亚洲一区免费视频| 国产在线ts人妖免费视频| 国产偷伦视频免费观看| 精品国产日韩久久亚洲| 亚洲性在线看高清h片| 91精品手机国产免费| 男人扒开添女人下部免费视频|