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

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

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

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

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

    導(dǎo)航

    統(tǒng)計(jì)

    常用鏈接

    留言簿

    隨筆檔案(14)

    文章分類(18)

    文章檔案(18)

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 久久久久久久亚洲Av无码| 亚洲国产成人片在线观看无码| 精品无码一区二区三区亚洲桃色| 国产精品午夜免费观看网站| 亚洲国产精品无码久久青草 | 亚洲国产精品VA在线看黑人| 无码人妻一区二区三区免费视频| 亚洲AV无码乱码在线观看牲色| 黄页视频在线观看免费| 免费又黄又爽又猛的毛片| 免费看黄福利app导航看一下黄色录像| 国产精品高清全国免费观看| 日韩在线观看免费| 亚洲国产成人精品无码久久久久久综合 | 亚洲精品日韩一区二区小说| 日本免费一区二区三区最新| 美女无遮挡免费视频网站| 狠狠色婷婷狠狠狠亚洲综合| 免费网站看av片| 亚洲最大的视频网站| 处破痛哭A√18成年片免费| 午夜亚洲WWW湿好爽| 亚洲天堂中文字幕在线| 中文成人久久久久影院免费观看| 亚洲A∨无码一区二区三区| 香蕉97超级碰碰碰免费公| 亚洲av日韩综合一区久热| 国产亚洲精品无码拍拍拍色欲 | 久久夜色精品国产噜噜亚洲a| 日韩在线视频免费看| 99精品视频在线观看免费| 亚洲视频在线免费看| 又大又粗又爽a级毛片免费看| 精品亚洲永久免费精品| 中文字幕在线观看亚洲视频| 亚洲精品无码永久在线观看 | 4399影视免费观看高清直播| 亚洲第一街区偷拍街拍| 亚洲精品无码国产| 天天看免费高清影视| 久久精品成人免费观看97|