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

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

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

    Java學習

    java,spring,structs,hibernate,jsf,ireport,jfreechart,jasperreport,tomcat,jboss -----本博客已經搬家了,新的地址是 http://www.javaly.cn 如果有對文章有任何疑問或者有任何不懂的地方,歡迎到www.javaly.cn (Java樂園)指出,我會盡力幫助解決。一起進步

     

    java解 哲學家就餐問題

    哲學家進餐問題是一個多線程運用的經典例子,涉及到線程同步/互斥,臨界區訪問問題以及一個避免死鎖的解決方法。

         有五個哲學家繞著圓桌坐,每個哲學家面前有一盤面,兩人之間有一支筷子,這樣每個哲學家左右各有一支筷子。

       哲學家有2個狀態,思考或者拿起筷子吃飯。如果哲學家拿到一只筷子,不能吃飯,直到拿到2只才能吃飯,并且一次只能拿起身邊的一支筷子。一旦拿起便不會放下筷子直到把飯吃完,此時才把這雙筷子放回原處。

       如果,很不幸地,每個哲學家拿起他或她左邊的筷子,那么就沒有人可以吃到飯了。這就會造成死鎖了。。這是需要堅決杜絕的,正如操作系統的死鎖問題。
                                                                                         
    代碼如下:
    import java.util.Random;
    public class DiningPhils
    {
    public static void main(String[] args)
    {
    int n = 10;
    if( n < 1)
    {
    System.err.println( "DiningPils <# of philosophers>" );
    System.exit(-1);
    }
    DiningPhils self = new DiningPhils();
    self.init(n);
    }

    public int getCount()
    {
    return n;
    }
    public void setChopstick( int i, boolean v)
    {
    chops[ i ] = v;
    }
    public boolean getChopstick( int i )
    {
    return chops[i];
    }

    private void init( final int N)
    {
    r = new Random();
    n = ( N < 0 || N > maxPhils ) ? maxPhils : N;
    chops = new boolean[n];
    phils = new Philosopher[n];
    initPhils();
    dumpStatus();
    }
    private void initPhils()
    {
    for( int i = 0; i< n; i++ )
    {
    phils[i] = new Philosopher( this, i );
    phils[i].setTimeSlice( generateTimeSlice());
    phils[i].setPriority( Thread.NORM_PRIORITY - 1);
    /**哲學家進程降低一級,使所有哲學家進程
    *全部初始化完畢前不會有哲學家進程搶占主線程*/
    }
    while( moreToStart() )
    {
    int i = Math.abs( r.nextInt()) % n;
    if( !phils[i].isAlive())
    {
    System.out.println( " ### Philosopher " +
    String.valueOf( i ) + " started.");
    phils[i].start();
    }
    }
    System.out.println( "\nPhilosophers Chopsticks"
    + "\n(1 = eating, 0 = thinking) (1 = taken, 0 = free)");
    }
    public int generateTimeSlice()
    {
    int ts = Math.abs(r.nextInt()) % (maxEat + 1);
    if( ts == 0 )
    ts = minEat;
    return ts;
    }
    public void dumpStatus()
    {
    for( int i = 0; i < n; i++)
    System.out.print(phils[i].getEat() ? 1 : 0);
    for( int i = n; i < maxPhils + 4; i++ )
    System.out.print(" ");
    for( int i = 0; i < n; i++)
    System.out.print(chops[i]? 1:0);
    System.out.println();
    }

    private boolean moreToStart()
    {
    for( int i = 0; i < phils.length; i++ )
    {
    if( !phils[i].isAlive())
    return true;
    }
    return false;
    }
    private int n;
    private Philosopher[] phils;
    private boolean[] chops;
    private Random r;
    private static final int maxPhils = 24; //最多哲學家數
    private static final int maxEat = 4; //最多進餐時間
    private static final int minEat = 1; //最少進餐時間
    }
    class Philosopher extends Thread
    {
    public Philosopher( DiningPhils HOST , int i )
    {
    host = HOST;
    index = i;
    }
    public void setTimeSlice( int TS )
    {
    ts = TS;
    }
    public void setLeftChopstick( boolean flag )
    {
    host.setChopstick(index, flag);
    }
    public void setRightChopstick( boolean flag )
    {
    host.setChopstick((index + 1)% host.getCount() , flag);
    }
    private void releaseChopsticks()
    {
    setLeftChopstick(false);
    setRightChopstick(false);
    }
    public boolean chopsticksFree()
    {
    return !host.getChopstick(index) &&
    !host.getChopstick((index+1)%host.getCount());
    }

    public void run()
    {
    while(true)
    {
    grabChopsticks();
    eat();
    think();
    }
    }
    private synchronized void grabChopsticks() /**臨界區函數,確保哲學家在沒有筷子或筷子不夠時思考,滿足條件后才就餐*/
    {
    while( !chopsticksFree())
    {
    try
    {
    wait();
    }
    catch( InterruptedException e){}
    }
    takeChopsticks();
    notifyAll();
    }
    private void takeChopsticks()
    {
    setLeftChopstick( true );
    setRightChopstick( true );
    setEat(true);
    host.dumpStatus();
    }
    private void eat()
    {
    pause();
    setEat( false );
    releaseChopsticks();
    }
    private void think()
    {
    pause();
    }
    private void pause()
    {
    setTimeSlice( host.generateTimeSlice());
    try
    {
    sleep(ts*1000);
    }
    catch( InterruptedException e){}
    }
    private void setEat(boolean v)
    {
    isEating = v;
    }
    public boolean getEat()
    {
    return isEating;
    }
    private DiningPhils host;
    private boolean isEating;
    private int index;
    private int ts;
    }

    posted on 2008-07-15 16:51 找個美女做老婆 閱讀(616) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     

    導航

    統計

    公告

    本blog已經搬到新家了, 新家:www.javaly.cn
     http://www.javaly.cn

    常用鏈接

    留言簿(6)

    隨筆檔案

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 午夜一区二区免费视频| 一区二区三区免费电影| 日本最新免费网站| 亚洲大尺度无码无码专区| 精品无码国产污污污免费网站国产 | 成人免费无遮挡无码黄漫视频| 亚洲黄色免费网址| 亚洲大片免费观看| 亚洲av永久无码精品三区在线4 | 亚洲精品无码专区| 热久久精品免费视频| 高h视频在线免费观看| 亚洲Av无码乱码在线观看性色| 污网站在线免费观看| 国产成人精品久久亚洲高清不卡 | 成人特级毛片69免费观看| 亚洲午夜爱爱香蕉片| 暖暖免费在线中文日本| 亚洲视频免费在线看| 在线观看免费污视频| 日韩在线视频免费| 亚洲av丰满熟妇在线播放| 99久久国产热无码精品免费 | 亚洲综合熟女久久久30p| 免费人成毛片动漫在线播放 | 在线观看免费精品国产| 久香草视频在线观看免费| 久久精品国产69国产精品亚洲| 最近中文字幕mv免费高清在线 | 亚洲精品亚洲人成在线观看麻豆| 无码人妻一区二区三区免费手机 | 日韩一级在线播放免费观看| 一级毛片免费毛片毛片| 91亚洲国产成人精品下载| 日本人的色道www免费一区| 美女无遮挡拍拍拍免费视频| 91亚洲国产成人久久精品| 四虎影视永久免费观看网址| 久久免费观看国产99精品| 亚洲精品成a人在线观看☆| 亚洲精品午夜无码专区|