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

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案(14)

          文章分類(18)

          文章檔案(18)

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 曲阜市| 湛江市| 柏乡县| 沅陵县| 长顺县| 密山市| 乌兰察布市| 吴桥县| 博爱县| 司法| 定陶县| 桂平市| 密云县| 保亭| 高阳县| 文安县| 青龙| 锡林浩特市| 中宁县| 称多县| 株洲市| 南康市| 阿荣旗| 余庆县| 招远市| 保亭| 平利县| 泗洪县| 新巴尔虎左旗| 元朗区| 利川市| 南乐县| 合作市| 江阴市| 靖远县| 新巴尔虎左旗| 曲阳县| 东乌珠穆沁旗| 贵定县| 临沧市| 思茅市|