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

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

    導航

    統計

    常用鏈接

    留言簿

    隨筆檔案(14)

    文章分類(18)

    文章檔案(18)

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲色成人四虎在线观看| 精品亚洲麻豆1区2区3区| 久久亚洲精品11p| 日韩免费毛片视频| 亚洲精品乱码久久久久久V| 四虎国产精品免费久久| 2020国产精品亚洲综合网| 在线观看视频免费国语| 亚洲AV无码无限在线观看不卡| 免费可以在线看A∨网站| 中文字幕亚洲综合久久综合| 成全视频在线观看免费高清动漫视频下载| 丁香婷婷亚洲六月综合色| 性色av免费观看| 国产亚洲精彩视频| 国产成人综合亚洲AV第一页| 野花香高清在线观看视频播放免费 | 国产高清视频免费在线观看 | 丁香花在线视频观看免费| 亚洲AV无码专区电影在线观看| 精品无码无人网站免费视频| 亚洲国产一区在线观看| 国产成人免费福利网站| 久久免费视频一区| 亚洲综合激情九月婷婷| 日本二区免费一片黄2019| 一本一道dvd在线观看免费视频| 亚洲国产精品一区二区久久hs| 麻豆视频免费播放| 午夜在线亚洲男人午在线| 亚洲成AV人片天堂网无码| 成年免费大片黄在线观看岛国| 日本亚洲中午字幕乱码| 亚洲国产成人一区二区精品区| 18勿入网站免费永久| 美女裸体无遮挡免费视频网站| 亚洲乱码中文字幕久久孕妇黑人| 99国产精品永久免费视频| 国产亚洲综合久久| 亚洲无人区视频大全| 亚洲国产小视频精品久久久三级|