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) 評(píng)論(0)  編輯  收藏 所屬分類: J2EE

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

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿

          隨筆檔案(14)

          文章分類(18)

          文章檔案(18)

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 大英县| 雷山县| 文昌市| 昆山市| 常州市| 织金县| 安新县| 宁都县| 临江市| 潍坊市| 五华县| 许昌市| 福建省| 涡阳县| 株洲市| 兴义市| 长葛市| 桂平市| 门源| 南部县| 南澳县| 通城县| 徐闻县| 福清市| 宣武区| 盐源县| 烟台市| 白沙| 高雄县| 阿鲁科尔沁旗| 清原| 浙江省| 新津县| 长汀县| 天等县| 遂溪县| 仁寿县| 大悟县| 阜宁县| 建湖县| 德清县|