• 哲学家问题(java)的三个解法


    //加synchronize进行同步
    //释放资源又很快获得自身的资源,这样不妥,吃完的话休息100ms
    
    //每个人先申请编号小的筷子
    
    public class Philosopher implements Runnable {
        int[] fork=new int[5];
        Thread thread1=new Thread(this,"1");
        Thread thread2=new Thread(this,"2");
        Thread thread3=new Thread(this,"3");
        Thread thread4=new Thread(this,"4");
        Thread thread5=new Thread(this,"5");
        public void run() {
            try {
                while (true) {
                    if (Thread.currentThread().getName().equals("1")) {
                        while (fork[0]==1) {
                            synchronized (this) {
                                wait();
                            }
                        }
                        fork[0]=1;
                        while (fork[1]==1) {
                            synchronized (this) {
                                wait();
                            }
                        }
                        fork[1]=1;
                        System.out.println("1 eats for 3 seconds");
                        Thread.sleep(3000);
                        fork[0]=0;
                        fork[1]=0;
                        synchronized(this) {
                            notifyAll();
                        }
                        Thread.sleep(100);
                    }
                    else if (Thread.currentThread().getName().equals("2")) {
                        while (fork[1]==1) {
                            synchronized(this) {
                                wait();
                            }
                        }
                        fork[1]=1;
                        while (fork[2]==1) {
                            synchronized(this) {
                                wait();
                            }
                        }
                        fork[2]=1;
                        System.out.println("2 eats for 3 seconds");
                        Thread.sleep(3000);
                        fork[1]=0;
                        fork[2]=0;
                        synchronized(this) {
                            notifyAll();
                        }
                        Thread.sleep(100);
                    }
                    else if (Thread.currentThread().getName().equals("3")) {
                        while (fork[2]==1) {
                            synchronized(this) {
                                wait();
                            }
                        }
                        fork[2]=1;
                        while (fork[3]==1) {
                            synchronized(this) {
                                wait();
                            }
                        }
                        fork[3]=1;
                        System.out.println("3 eats for 3 seconds");
                        Thread.sleep(3000);
                        fork[2]=0;
                        fork[3]=0;
                        synchronized(this) {
                            notifyAll();
                        }
                        Thread.sleep(100);
                    }
                    else if (Thread.currentThread().getName().equals("4")) {
                        while (fork[3]==1) {
                            synchronized(this) {
                                wait();
                            }
                        }
                        fork[3]=1;
                        while (fork[4]==1) {
                            synchronized(this) {
                                wait();
                            }
                        }
                        fork[4]=1;
                        System.out.println("4 eats for 3 seconds");
                        Thread.sleep(3000);
                        fork[3]=0;
                        fork[4]=0;
                        synchronized(this) {
                            notifyAll();
                        }
                        Thread.sleep(100);
                    }
                    else if (Thread.currentThread().getName().equals("5")) {
                        while (fork[0]==1) {
                            synchronized(this) {
                                wait();
                            }
                        }
                        fork[0]=1;
                        while (fork[4]==1) {
                            synchronized(this) {
                                wait();
                            }
                        }
                        fork[4]=1;
                        System.out.println("5 eats for 3 seconds");
                        Thread.sleep(3000);
                        fork[0]=0;
                        fork[4]=0;
                        synchronized(this) {
                            notifyAll();
                        }
                        Thread.sleep(100);
                    }            
                }
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            Philosopher phi=new Philosopher();
            for (int i=0;i<5;i++)
                phi.fork[i]=0;
            phi.thread1.start();
            phi.thread2.start();
            phi.thread3.start();
            phi.thread4.start();
            phi.thread5.start();
        }
    }
    
    //当某个线程试图等待一个自己并不拥有的对象(O)的监控器或者通知其他线程等待该对象(O)的监控器时,抛出该异常。
    //让刚吃完的一个人阻塞,5根筷子供4个人选,则必有一个人获得在其左右的两双筷子
    
    public class Philosopher1 implements Runnable {
        int[] ifeat=new int[5];
        int[] fork=new int[5];
        int noteat;
        Thread thread1=new Thread(this,"1");
        Thread thread2=new Thread(this,"2");
        Thread thread3=new Thread(this,"3");
        Thread thread4=new Thread(this,"4");
        Thread thread5=new Thread(this,"5");
        public void run() {
            try {
                while (true) {
                    if (Thread.currentThread().getName().equals("1")) {
                        while (ifeat[0]==1) {
                            synchronized (this) {
                                wait();
                            }
                        }
                        while (fork[0]==1) {
                            synchronized (this) {
                                wait();
                            }
                        }
                        fork[0]=1;
                        while (fork[1]==1) {
                            synchronized (this) {
                                wait();
                            }
                        }
                        fork[1]=1;
                        System.out.println("1 eats for 3 seconds");
                        Thread.sleep(3000);
                        fork[0]=0;
                        fork[1]=0;
                        ifeat[noteat]=0;
                        noteat=0;
                        ifeat[0]=1;                    
                        synchronized(this) {
                            notifyAll();
                        }
                        Thread.sleep(100);
                    }
                    else if (Thread.currentThread().getName().equals("2")) {
                        while (ifeat[1]==1) {
                            synchronized (this) {
                                wait();
                            }
                        }
                        while (fork[1]==1) {
                            synchronized (this) {
                                wait();
                            }
                        }
                        fork[1]=1;
                        while (fork[2]==1) {
                            synchronized (this) {
                                wait();
                            }
                        }
                        fork[2]=1;
                        System.out.println("2 eats for 3 seconds");
                        Thread.sleep(3000);
                        fork[1]=0;
                        fork[2]=0;
                        ifeat[noteat]=0;
                        noteat=1;                    
                        ifeat[1]=1;
                        synchronized(this) {
                            notifyAll();
                        }
                        Thread.sleep(100);                
                    }
                    else if (Thread.currentThread().getName().equals("3")) {
                        while (ifeat[2]==1) {
                            synchronized (this) {
                                wait();
                            }
                        }        
                        while (fork[2]==1) {
                            synchronized (this) {
                                wait();
                            }
                        }
                        fork[2]=1;
                        while (fork[3]==1) {
                            synchronized (this) {
                                wait();
                            }
                        }
                        fork[3]=1;
                        System.out.println("3 eats for 3 seconds");
                        Thread.sleep(3000);
                        fork[2]=0;
                        fork[3]=0;
                        ifeat[noteat]=0;
                        noteat=2;
                        ifeat[2]=1;
                        synchronized(this) {
                            notifyAll();
                        }
                        Thread.sleep(100);            
                    }
                    else if (Thread.currentThread().getName().equals("4")) {
                        while (ifeat[3]==1) {
                            synchronized (this) {
                                wait();
                            }
                        }
                        while (fork[3]==1) {
                            synchronized (this) {
                                wait();
                            }
                        }
                        fork[3]=1;
                        while (fork[4]==1) {
                            synchronized (this) {
                                wait();
                            }
                        }
                        fork[4]=1;
                        System.out.println("4 eats for 3 seconds");
                        Thread.sleep(3000);
                        fork[3]=0;
                        fork[4]=0;
                        ifeat[noteat]=0;
                        noteat=3;
                        ifeat[3]=1;
                        synchronized(this) {
                            notifyAll();
                        }
                        Thread.sleep(100);
                    }
                    else if (Thread.currentThread().getName().equals("5")) {
                        while (ifeat[4]==1) {
                            synchronized (this) {
                                wait();
                            }
                        }
                        while (fork[4]==1) {
                            synchronized (this) {
                                wait();
                            }
                        }
                        fork[4]=1;
                        while (fork[0]==1) {
                            synchronized (this) {
                                wait();
                            }
                        }
                        fork[0]=1;
                        System.out.println("5 eats for 3 seconds");
                        Thread.sleep(3000);
                        fork[4]=0;
                        fork[0]=0;
                        ifeat[noteat]=0;
                        noteat=4;
                        ifeat[4]=1;
                        synchronized(this) {
                            notifyAll();
                        }
                        Thread.sleep(100);
                    }
                }
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            Philosopher1 phi=new Philosopher1();
            for (int i=0;i<5;i++)
                phi.fork[i]=0;
            
            phi.ifeat[0]=1;
            for (int i=0;i<5;i++)
                phi.ifeat[i]=0;
            phi.noteat=0;
            
            phi.thread1.start();
            phi.thread2.start();
            phi.thread3.start();
            phi.thread4.start();
            phi.thread5.start();
        }
    }
    //只有两双筷子都有,才获取,且同时获取两双筷子
    
    public class Philosopher2 implements Runnable {
        int[] fork=new int[5];
        Thread thread1=new Thread(this,"1");
        Thread thread2=new Thread(this,"2");
        Thread thread3=new Thread(this,"3");
        Thread thread4=new Thread(this,"4");
        Thread thread5=new Thread(this,"5");
        public void run() {
            try {
                while (true) {
                    if (Thread.currentThread().getName().equals("1")) {
                        while (fork[0]==1 || fork[1]==1) {
                            synchronized (this) {
                                wait();
                            }
                        }
                        fork[0]=1;
                        fork[1]=1;
                        System.out.println("1 eats for 3 seconds");
                        Thread.sleep(3000);
                        fork[0]=0;
                        fork[1]=0;
                        synchronized(this) {
                            notifyAll();
                        }
                        Thread.sleep(100);
                    }
                    else if (Thread.currentThread().getName().equals("2")) {
                        while (fork[1]==1 || fork[2]==1) {
                            synchronized (this) {
                                wait();
                            }
                        }
                        fork[1]=1;
                        fork[2]=1;
                        System.out.println("2 eats for 3 seconds");
                        Thread.sleep(3000);
                        fork[1]=0;
                        fork[2]=0;
                        synchronized(this) {
                            notifyAll();
                        }
                        Thread.sleep(100);                
                    }
                    else if (Thread.currentThread().getName().equals("3")) {
                        while (fork[2]==1 || fork[3]==1) {
                            synchronized (this) {
                                wait();
                            }
                        }
                        fork[2]=1;
                        fork[3]=1;
                        System.out.println("3 eats for 3 seconds");
                        Thread.sleep(3000);
                        fork[2]=0;
                        fork[3]=0;
                        synchronized(this) {
                            notifyAll();
                        }
                        Thread.sleep(100);
                    }
                    else if (Thread.currentThread().getName().equals("4")) {
                        while (fork[3]==1 || fork[4]==1) {
                            synchronized (this) {
                                wait();
                            }
                        }
                        fork[3]=1;
                        fork[4]=1;
                        System.out.println("4 eats for 3 seconds");
                        Thread.sleep(3000);
                        fork[3]=0;
                        fork[4]=0;
                        synchronized(this) {
                            notifyAll();
                        }
                        Thread.sleep(100);
                    }
                    else if (Thread.currentThread().getName().equals("5")) {
                        while (fork[0]==1 || fork[4]==1) {
                            synchronized (this) {
                                wait();
                            }
                        }
                        fork[0]=1;
                        fork[4]=1;
                        System.out.println("5 eats for 3 seconds");
                        Thread.sleep(3000);
                        fork[0]=0;
                        fork[4]=0;
                        synchronized(this) {
                            notifyAll();
                        }
                        Thread.sleep(100);
                    }            
                }
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            Philosopher2 phi=new Philosopher2();
            for (int i=0;i<5;i++)
                phi.fork[i]=0;
            phi.thread1.start();
            phi.thread2.start();
            phi.thread3.start();
            phi.thread4.start();
            phi.thread5.start();
        }
    }
  • 相关阅读:
    python测试开发django-165.form表单序列化json的2种方式
    python测试开发django-164.bootstrap-table 单元格添加select下拉框
    python测试开发django-163.bootstrap-table 表格单元格行内编辑
    python测试开发django-162.ajax 提交表单,防重复提交(beforeSend)
    postman使用教程18-如何取出返回 cookie 中的 sessionId 值
    python测试开发django-161.Celery 定时任务保存到数据库 (djcelery)
    python测试开发django-160.Celery 定时任务 (beat)
    pytest文档77
    python测试开发django-159.Celery 异步与 RabbitMQ 环境搭建
    计算图像数据集的均值和方差(mean, std)用于transforms.Normalize()标准化
  • 原文地址:https://www.cnblogs.com/cmyg/p/8159002.html
Copyright © 2020-2023  润新知