home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / applets / dining~1 / dining~1.jav < prev    next >
Encoding:
Text File  |  1995-10-31  |  12.3 KB  |  522 lines

  1. /* 
  2. java jode 
  3. this is an applet that demonstraits the dining philosophers problem
  4. */
  5.  
  6. import java.awt.Graphics; 
  7. import java.awt.*;
  8. import java.net.*;
  9. import java.io.InputStream;
  10.  
  11. public class DiningPhilosophers extends java.applet.Applet implements Runnable 
  12.   {
  13.     String audio;
  14.     Image bullet;
  15.     Image phil[] = new Image[5];
  16.     Image oldphil[] = new Image[5];
  17.     Image thinkingphil[][] = new Image[5][4];
  18.     Image hungryphil[][] = new Image[5][4];
  19.     Image eatingphil[][] = new Image[5][4];
  20.     Image background;
  21.     int slide1=185,slide2=185,slide3=185,oldslide1,oldslide2,oldslide3;
  22.     boolean slide1click=false, slide2click=false, slide3click=false;
  23.     sticks Sticks[] = new sticks[5];
  24.     int thishand[] = new int[5];
  25.     int thathand[] = new int[5];
  26.     Thread au=null;
  27.     Thread bu=null;
  28.     Thread cu=null;
  29.     Thread du=null;
  30.     Thread eu=null;
  31.     Thread fu=null;
  32.     int ticketnumber=0;
  33.  
  34.     float EatChance=.25f;
  35.     float ThinkChance=.75f;
  36.     int speed=80;
  37.  
  38.     final int xoffset =-80 ,yoffset=-100;
  39.     final int buttonx=xoffset+80+73, buttony=yoffset+138+398, buttonspace=90;
  40.     final int slide1y=yoffset+138+433, slide2y=yoffset+138+463, slide3y=yoffset+138+493;
  41.     final int slidexl=xoffset+80+60,slidexr=xoffset+80+430;
  42.  
  43.     public void init() 
  44.       {
  45.     audio="eat";
  46.  
  47.     oldslide1=slide1;
  48.     oldslide2=slide2;
  49.     oldslide3=slide3;
  50.  
  51.     Sticks[0]=new sticks();
  52.     Sticks[1]=new sticks();
  53.     Sticks[2]=new sticks();
  54.     Sticks[3]=new sticks();
  55.     Sticks[4]=new sticks();
  56.  
  57.     Sticks[0].returnStick(7);
  58.     Sticks[1].returnStick(3);
  59.     Sticks[2].returnStick(3);
  60.     Sticks[3].returnStick(3);
  61.     Sticks[4].returnStick(3);
  62.  
  63.  
  64.     au=new Thread(this);
  65.     bu=new Thread(this);
  66.     cu=new Thread(this);
  67.     du=new Thread(this);
  68.     eu=new Thread(this);
  69.     fu=new Thread(this);
  70.     System.out.println("we think, therefore we are");
  71.  
  72.     au.start();
  73.     bu.start();
  74.     cu.start();
  75.     du.start();
  76.     eu.start();
  77.     fu.start();
  78.  
  79.  
  80.  
  81.     stop();
  82.     initializeImages();
  83.       }
  84.  
  85.     public void start()
  86.       {
  87.     System.out.println("we live");
  88.  
  89.     au.resume();
  90.     bu.resume();
  91.     cu.resume();
  92.     du.resume();
  93.     eu.resume();
  94.     fu.resume();
  95.       }
  96.  
  97.     public void stop()
  98.       {
  99.     System.out.println("help, they're comming to stop us");
  100.  
  101.     au.suspend();
  102.     bu.suspend();
  103.     cu.suspend();
  104.     du.suspend();
  105.     eu.suspend();
  106.     fu.suspend();
  107.       }
  108.     public void destroy()
  109.       {
  110.     System.out.println("help, they're comming to killing us!");
  111.  
  112.     au.stop();
  113.     bu.stop();
  114.     cu.stop();
  115.     du.stop();
  116.     eu.stop();
  117.     fu.stop();
  118.  
  119. /* wake up threads so they can die */
  120.  
  121.     au.resume();
  122.     bu.resume();
  123.     cu.resume();
  124.     du.resume();
  125.     eu.resume();
  126.     fu.resume();
  127.  
  128.     Sticks[0].returnStick(3);
  129.     Sticks[1].returnStick(3);
  130.     Sticks[2].returnStick(3);
  131.     Sticks[3].returnStick(3);
  132.     Sticks[4].returnStick(3);
  133.  
  134.       }
  135.  
  136.     Graphics offg;
  137.     Image offimg;
  138.  
  139.     public void paint(Graphics g)
  140.       {
  141.       if (background == null) {
  142.           return;
  143.       }
  144.  
  145.       Dimension d = size();
  146. /*only draw things that might have changed */
  147.       if (offimg == null) {
  148.           offimg = createImage(d.width, d.height);
  149.           offg = offimg.getGraphics();
  150.       }
  151.       
  152.       offg.setColor(getBackground());
  153.       offg.fillRect(0, 0, d.width, d.height);
  154.       offg.setColor(Color.black);
  155.  
  156.         offg.drawImage(background,xoffset+80,yoffset+138, this);
  157.         offg.drawImage(bullet, buttonx, buttony, this);
  158.         offg.drawImage(bullet, buttonx+buttonspace, buttony, this);
  159.         offg.drawImage(bullet, buttonx+buttonspace*2, buttony, this);
  160.         offg.drawImage(bullet, buttonx+buttonspace*3, buttony, this);
  161.         offg.drawString("Speed",xoffset+80,slide1y+12);
  162.         offg.drawString("Think",xoffset+80,slide2y+12);
  163.         offg.drawString("Talk ",xoffset+80,slide3y+12);
  164.         offg.drawLine(slidexl,slide1y+8,slidexr,slide1y+8);
  165.         offg.drawLine(slidexl,slide2y+8,slidexr,slide2y+8);
  166.         offg.drawLine(slidexl,slide3y+8,slidexr,slide3y+8);
  167.  
  168.     offg.drawImage(phil[0],xoffset+216,yoffset+181, this);
  169.     offg.drawImage(phil[1],xoffset+335,yoffset+187, this);
  170.     offg.drawImage(phil[2],xoffset+379,yoffset+261, this);
  171.     offg.drawImage(phil[3],xoffset+245,yoffset+318, this);
  172.     offg.drawImage(phil[4],xoffset+138,yoffset+247, this);
  173.  
  174.       //offg.clearRect(slidexl+oldslide1, slide1y,16,16);
  175.         offg.drawLine(slidexl+oldslide1,slide1y+8,slidexl+oldslide1+16,slide1y+8);
  176.         offg.drawImage(bullet, slidexl+slide1, slide1y, this);
  177.         oldslide1=slide1;
  178.  
  179.       //offg.clearRect(slidexl+oldslide2, slide2y,16,16);
  180.         offg.drawLine(slidexl+oldslide2,slide2y+8,slidexl+oldslide2+16,slide2y+8);
  181.         offg.drawImage(bullet, slidexl+slide2, slide2y, this);
  182.         oldslide2=slide2;
  183.  
  184.       //offg.clearRect(slidexl+oldslide3, slide3y,16,16);
  185.         offg.drawLine(slidexl+oldslide3,slide3y+8,slidexl+oldslide3+16,slide3y+8);
  186.         offg.drawImage(bullet, slidexl+slide3, slide3y, this);
  187.         oldslide3=slide3;
  188.  
  189.       g.drawImage(offimg, 0, 0, null);
  190.       }
  191.  
  192.     public void update(Graphics g)
  193.       {
  194.     /* pretty cheesey, but gets rid of flash */
  195.     paint(g);
  196.       }
  197.  
  198.     public void run()
  199.       {
  200. /* starts threads, one for each phil, + one do keep track of whats up */
  201.     int number;
  202.  
  203.  
  204.     Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  205.     number=getTicket();
  206.     
  207.     if(number>4) whatsup();
  208.     else philosopher(number);
  209.       }
  210.  
  211.     public boolean mouseDown(java.awt.Event evt, int x, int y)
  212.       {
  213. /* grab bullet */
  214.     int by2;
  215.  
  216.     if(y<20)
  217.       {
  218.         System.out.println("eh?");
  219.         System.out.println("number of threads:"+Thread.activeCount());
  220.       }
  221.  
  222.     if((y>buttony)&&(y<buttony+16))
  223.        {
  224.          if((x>buttonx)&&(x<buttonx+16)) audio="eat";
  225.          if((x>buttonx+buttonspace)&&(x<buttonx+buttonspace+16)) audio="talk";
  226.          if((x>buttonx+buttonspace*2)&&(x<buttonx+buttonspace*2+16)) audio="laugh";
  227.          if((x>buttonx+buttonspace*3)&&(x<buttonx+buttonspace*3+16)) audio=null;
  228.        }
  229.  
  230.     if((y>60)&&(y<76)&&(x>(slide3-8))&&(x<(slide3+8))) slide3click=true;
  231.  
  232.     y=y-slide1y;
  233.     x=x-slidexl-8;
  234.  
  235.  
  236.     if((y>0 )&&(y<16)&&(x>(slide1-8))&&(x<(slide1+8))) slide1click=true;
  237.     if((y>30)&&(y<46)&&(x>(slide2-8))&&(x<(slide2+8))) slide2click=true;
  238.     if((y>60)&&(y<76)&&(x>(slide3-8))&&(x<(slide3+8))) slide3click=true;
  239.  
  240.     
  241.     return true;
  242.       }
  243.  
  244.     public boolean mouseExit(java.awt.Event evt)
  245.       {
  246. /* let go bullet */
  247.     slide1click=false;
  248.     slide2click=false;
  249.     slide3click=false;
  250.     return true;
  251.       }
  252.  
  253.     public boolean mouseUp(java.awt.Event evt, int x, int y)
  254.       {
  255. /* let go bullet */
  256.     slide1click=false;
  257.     slide2click=false;
  258.     slide3click=false;
  259.     return true;
  260.       }
  261.  
  262.     public boolean mouseDrag(java.awt.Event evt, int x, int y)
  263.       {
  264. /* handles sliders, and sets parameters */
  265.     y=y-slide1y;
  266.     x=x-slidexl-8;
  267.     int len=slidexr-slidexl-16;
  268.  
  269.         if(slide1click)
  270.           {
  271.         if(x<=0) x=0;
  272.         if(x>=len) x=len;
  273.         speed=len-x;
  274.         speed=(speed*speed)/len;
  275.         slide1=x;
  276.           }
  277.  
  278.  
  279.         if(slide2click)
  280.           {
  281.         if(x<=0) x=0;
  282.         if(x>=len) x=len;
  283.         ThinkChance=(float)x/len;
  284.         ThinkChance*=ThinkChance;
  285.         ThinkChance=1-ThinkChance;
  286.         slide2=x;
  287.           }
  288.  
  289.         if(slide3click)
  290.           {    
  291.         if(x<=0) x=0;
  292.         if(x>=len) x=len;
  293.         EatChance=1-(float)x/len;
  294.         EatChance=EatChance*EatChance;
  295.         slide3=x;
  296.           }
  297.     return true;
  298.       }
  299.  
  300.     synchronized int getTicket()
  301.       {
  302. /* so each philnumber will be unique */    
  303.     return ticketnumber++;
  304.       }
  305.  
  306.  
  307.     void initializeImages()
  308.       {
  309. /* load in lots of pretty pictures */
  310.     int i,j;
  311.  
  312.     bullet=this.getImage(getCodeBase(), "images/b2.gif");
  313.     if(bullet==null) System.out.println("no bullet");
  314.  
  315.     background=this.getImage(getCodeBase(), "images/background.gif");
  316.     if(background==null) System.out.println("no picture ");
  317.  
  318.     for(i=0;i<5;i++)
  319.       {
  320.  
  321.         for(j=0;j<4;j++)
  322.           {
  323.         thinkingphil[i][j]=this.getImage(getCodeBase(), "images/thinking"+j+"."+i+".gif");
  324.         if(thinkingphil[i][j]==null)
  325.           System.out.println("no picture "+i);
  326.           }
  327.  
  328.         for(j=0;j<4;j++)
  329.           {
  330.         hungryphil[i][j]=this.getImage(getCodeBase(), "images/hungry"+j+"."+i+".gif");
  331.         if(hungryphil[i][j]==null) System.out.println("no picture "+i);
  332.           }
  333.  
  334.         for(j=0;j<4;j++)
  335.           {
  336.         eatingphil[i][j]=this.getImage(getCodeBase(), "images/eating"+j+"."+i+".gif");
  337.         if(eatingphil[i][j]==null)
  338.           System.out.println("no picture "+i);
  339.           }
  340.  
  341.       }
  342.     phil[0]=thinkingphil[0][0];
  343.     phil[1]=thinkingphil[1][0];
  344.     phil[2]=thinkingphil[2][0];
  345.     phil[3]=thinkingphil[3][0];
  346.     phil[4]=thinkingphil[4][0];
  347.  
  348.       }
  349.  
  350.     void philosopher(int mynumber)
  351.       {
  352. /* the phil process */    
  353.     System.out.println(mynumber);
  354.     for(;;)
  355.       {
  356.         think(mynumber);
  357.         PickUpSticks(mynumber);
  358.         eat(mynumber);
  359.                DropSticks(mynumber);
  360.       }
  361.       }
  362.     
  363.     void think(int mynumber)
  364.       {
  365.     int i=0;
  366.     
  367.     while(i<4)
  368.       {
  369.         phil[mynumber]=thinkingphil[mynumber][i];
  370.         i+= (Math.random()<ThinkChance) ? 1 : -1;
  371.     try {Thread.sleep(speed*8);} catch (InterruptedException e){}
  372.         if(i<0) i=0;
  373.       }
  374.     phil[mynumber]=thinkingphil[mynumber][0];
  375.        
  376.       }
  377.  
  378.     void eat(int mynumber)
  379.       {
  380.     do
  381.       {
  382.         phil[mynumber]=eatingphil[mynumber][0];
  383.     try {Thread.sleep(speed);} catch (InterruptedException e){}
  384.         phil[mynumber]=eatingphil[mynumber][1];
  385.     try {Thread.sleep(speed);} catch (InterruptedException e){}
  386.         phil[mynumber]=eatingphil[mynumber][2];
  387.  
  388.         if(audio!=null) play(getCodeBase(), "audio/"+audio+mynumber+".au");
  389.  
  390.     try {Thread.sleep(speed);} catch (InterruptedException e){}
  391.         phil[mynumber]=eatingphil[mynumber][3];
  392.     try {Thread.sleep(speed);} catch (InterruptedException e){}
  393.         phil[mynumber]=eatingphil[mynumber][2];
  394.     try {Thread.sleep(speed);} catch (InterruptedException e){}
  395.         phil[mynumber]=eatingphil[mynumber][1];
  396.     try {Thread.sleep(speed);} catch (InterruptedException e){}
  397.         phil[mynumber]=eatingphil[mynumber][0];
  398.     try {Thread.sleep(speed);} catch (InterruptedException e){}
  399.         phil[mynumber]=thinkingphil[mynumber][0];
  400.     try {Thread.sleep(speed);} catch (InterruptedException e){}
  401.       } while(Math.random()>EatChance);
  402.       }
  403.  
  404.     void PickUpSticks(int n)
  405.       {
  406. /*  picks up stickes
  407. a phil must have both sticks before it can eat*/
  408.  
  409.     int thisside,thatside;
  410.     if(.5f>Math.random())
  411.       {
  412.         thisside= (n<4) ? n+1 : 0;
  413.         thatside= n;
  414.       }
  415.     else
  416.       {
  417.         thatside= (n<4) ? n+1 : 0;
  418.         thisside= n;
  419.       }
  420. /* pick up a stick */
  421.     phil[n]=hungryphil[n][1];
  422.     try {Thread.sleep(speed);} catch (InterruptedException e){}
  423.     thishand[n]=Sticks[thisside].waitForStick();
  424.     if(thishand[n]==7)
  425.       {
  426. /* if its the magic stick, drop, and pick up other sticks */
  427.         phil[n]=hungryphil[n][2];
  428.     try {Thread.sleep(speed);} catch (InterruptedException e){}
  429.         Sticks[thisside].returnStick(thishand[n]);
  430. /* a deadlock is possible if the magic stick goes all the way around the 
  431.    table to "thathand",  dont think it can happen though */
  432.  
  433.  
  434.         thathand[n]=Sticks[thatside].waitForStick();
  435.  
  436.         phil[n]=hungryphil[n][3];
  437.     try {Thread.sleep(speed);} catch (InterruptedException e){}
  438.         thishand[n]=Sticks[thisside].waitForStick();
  439.  
  440.         phil[n]=hungryphil[n][0];
  441.     try {Thread.sleep(speed);} catch (InterruptedException e){}
  442.         
  443.       }
  444.     else
  445.       { 
  446. /* get other stick */
  447.         phil[n]=hungryphil[n][0];
  448.     try {Thread.sleep(speed);} catch (InterruptedException e){}
  449.         thathand[n]=Sticks[thatside].waitForStick();
  450.       }
  451.       }
  452.  
  453.     void DropSticks(int n)
  454.       {
  455. /* drop sticks */
  456.     int thisside,thatside;
  457.     if(.5f>Math.random())
  458.       {
  459.         thisside= (n<4) ? n+1 : 0;
  460.         thatside= n;
  461.       }
  462.     else
  463.       {
  464.         thatside= (n<4) ? n+1 : 0;
  465.         thisside= n;
  466.       }
  467.  
  468.            Sticks[thisside].returnStick(thathand[n]);
  469.     Sticks[thatside].returnStick(thishand[n]);
  470.       }
  471.  
  472.     
  473.  
  474.     void whatsup()
  475.       {
  476. /* repaints the scene */
  477.     for(;;)
  478.       {
  479.     try {Thread.sleep(speed);} catch (InterruptedException e){}
  480.         repaint();
  481.       }
  482.       }
  483.  
  484.   }
  485.  
  486. /* an instance of the class is created for each stick */
  487. class sticks
  488. {
  489.   int Stick=0;
  490.  
  491.   int waitForStick()
  492.     {
  493.       return stickstuff(0);
  494.     }
  495.   void returnStick(int s)
  496.     {
  497.       stickstuff(s);
  498.     }
  499.  
  500.   synchronized int stickstuff( int s)
  501. /* make sure phils dont read and write at the same time */
  502.     {
  503.       if(s!=0)
  504.     {
  505.       Stick=s;
  506.       notify();
  507.       return 0;
  508.     }
  509.       else
  510.     {
  511.       if(Stick<1)
  512.         {
  513.           Stick=-4;
  514.           try {wait();} catch (InterruptedException e) {}
  515.         }
  516.       s=Stick;
  517.       Stick=0;
  518.       return s;
  519.     }
  520.     }
  521. }
  522.