Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package DINING_PHILOSOPHER;
- public class Philosopher extends Thread{
- public static int Total=0;//tong so thread khoi tao
- public static int runTotal=0;//tong so thread dang chay
- private Chopstick left,right;
- private String name;
- public Philosopher(String name,Chopstick left,Chopstick right){
- this.name=name;
- this.left=left;
- this.right=right;
- Total++;
- }
- public void eat(){
- synchronized(left){
- synchronized(right){
- left.use();
- right.use();
- System.out.println(name+" nhat dua");
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- }
- }
- }
- think();
- }
- public void think(){
- System.out.println(name+" bo dua");
- left.release();
- right.release();
- this.runTotal--;
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- }
- }
- public void run(){
- for(int i=1;i<=10;i++)
- {
- while(true){
- if((Total-runTotal)!=1){
- this.runTotal++;
- eat();
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement