Philosopher Example - PowerPoint PPT Presentation

About This Presentation
Title:

Philosopher Example

Description:

class Philosopher implements Runnable { private int id; private PhilosopherShare share; ... id ' is thinking'); share.thinkOrEat(); share.pickUp(id) ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 4
Provided by: matt51
Category:

less

Transcript and Presenter's Notes

Title: Philosopher Example


1
Philosopher Example
  • class PhilosophersShare
  • int state new int6
  • static final int THINKING 0
  • static final int HUNGRY 1
  • static final int EATING 2
  • public Philosopher()
  • for(int i0 ilt6 i) statei THINKING
  • public synchronized void pickUp(int id)
  • stateid HUNGRY
  • test(id)
  • while(stateid ! EATING)
  • try wait()
  • catch(InterruptedException e)
  • public synchronized void putDown(int id)
  • stateid THINKING
  • test((id 5) 6)

2
Philosopher Example
  • class Philosopher implements Runnable
  • private int id
  • private PhilosopherShare share
  • public Philosopher(int id, PhilosopherShare
    share)
  • this.id id
  • this.share share
  • public void run()
  • for()
  • System.out.println(Philosopher id
    is thinking)
  • share.thinkOrEat()
  • share.pickUp(id)
  • System.out.println(Philosopher id
    is eating)
  • share.thinkOrEat()
  • share.putDown(id)

3
Philosopher Example
  • class DiningPhilosophers
  • public static void main(String args)
  • PhilosopherShare share new PhilosopherShare()
  • Philosopher p new Philosopher6
  • for(int i0 ilt6 i)
  • pi new Philosopher(i, share)
  • Thread t new Thread6
  • for(int i0 ilt6 i)
  • ti new Thread(pi)
  • ti.start()
Write a Comment
User Comments (0)
About PowerShow.com