Concurrency - sleeping priority

          Sleeping
          A simple way to affect the behavior of your tasks is by calling sleep( ) to cease (block) the execution of that task for a given time.
          If you must control the order of execution of tasks, your best bet is to use synchronization controls.
          public class SleepingTask extends LiftOff{
              
          public void run(){
                  
          try {
                      
          while(countDown-- > 0){
                          System.out.print(status());
                          
          // Old-style:
                          
          // Thread.sleep(100);
                          
          // Java SE5/6-style
                          TimeUnit.MICROSECONDS.sleep(100);
                      }
                  } 
          catch (InterruptedException e) {
                      
          // TODO: handle exception
                  }
              }
              
              
          public static void main(String[] args)  {
                  ExecutorService exec 
          = Executors.newCachedThreadPool();
                  
          for(int i=0; i<5; i++)
                      exec.execute(
          new SleepingTask());
                  exec.shutdown();
              }
          }


          Priority
          The priority of a thread conveys the importance of a thread to the scheduler.
          However, this doesn’t mean that threads with lower priority aren’t run (so you can’t get deadlocked because of priorities).
          Lower-priority threads just tend to run less often.
          Trying to manipulate thread priorities is usually a mistake.

          Although the JDK has 10 priority levels, this doesn’t map well to many operating systems.
          public class SimplePriorities implements Runnable {
              
          private int countDown = 5;
              
          private volatile double d; //No optimization
              private int priority;
              
          public SimplePriorities(int priority){
                  
          this.priority = priority;
              }
              
              
          public String toString(){
                  
          return Thread.currentThread() + "" + countDown;
              }

              @Override
              
          public void run() {
                  Thread.currentThread().setPriority(priority);
                  
          while(true){
                      
          // An expensive, interruptable operation
                      for(int i=1; i<10000; i++){
                          d 
          += (Math.PI + Math.E)/(double)i;
                          
          if(i%1000 == 0)
                              Thread.yield();
                      }
                      System.out.println(
          this);
                      
          if(--countDown ==0)
                          
          return;
                  }
              }

              
          public static void main(String[] args) {
                  ExecutorService exec 
          = Executors.newCachedThreadPool();
                  
          for(int i=0; i<5; i++)
                      exec.execute(
          new SimplePriorities(Thread.MIN_PRIORITY));
                  exec.execute(
          new SimplePriorities(Thread.MAX_PRIORITY));
                  exec.shutdown();
              }
          }
















          posted on 2012-11-21 10:56 鹽城小土包 閱讀(146) 評論(0)  編輯  收藏 所屬分類: J2EE

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

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案(14)

          文章分類(18)

          文章檔案(18)

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 上饶市| 石渠县| 大埔区| 厦门市| 镇康县| 河东区| 海阳市| 荥阳市| 旺苍县| 株洲县| 福泉市| 沙坪坝区| 温宿县| 吴忠市| 瓮安县| 南乐县| 泰顺县| 麟游县| 巴东县| 北安市| 清新县| 佳木斯市| 富裕县| 望谟县| 弋阳县| 临夏县| 明水县| 密云县| 即墨市| 隆德县| 东明县| 玉林市| 荃湾区| 囊谦县| 辽源市| 罗源县| 如东县| 康保县| 祁连县| 永寿县| 云阳县|