尝试实现多线程,但只有一个线程在做工作

时间:2019-03-31 20:49:41

标签: java multithreading

我正在尝试用Java中的两个线程来实现Dekker算法。但是由于某种原因,只有一个线程在做这项工作。请帮帮我。

public class Dekkers  {
    static int turn = 0;
    static boolean [] flag = {false,false};
    public static class thread1 implements Runnable{
        @Override
        public void run() {         
            for(int i = 0; i<5;i++) {
                flag[0]=true;
                while(flag[1]) {
                    if(turn ==1) {
                        flag[0]=false;
                        while (turn==1) {}
                        flag[0]=true;}}
                        Random rand =new Random();
                        System.out.println("We hold these truths to be self-evident, that all men are created equal,1");
                        try {
                            Thread.sleep(1000000000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        System.out.println("that they are endowed by their Creator with certain unalienable Rights,1");
                        try {
                            Thread.sleep(rand.nextInt(20));
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        System.out.println("that among these are Life, Liberty and the pursuit of Happiness.1");
                        try {
                            Thread.sleep(rand.nextInt(20));
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        turn = 1;
                        flag[0]=false;




            }
        }
    }

    public static class thread2 implements Runnable{
        @Override
        public void run() {         
            for(int i = 0; i<5;i++) {
                flag[1]=true;
                while(flag[0]) {
                    if(turn ==0) {
                        flag[1]=false;
                        System.out.print("changed");
                        while (turn==0) {}
                        flag[1]=true;}}
                        Random rand =new Random();
                        System.out.println("We hold these truths to be self-evident, that all men are created equal,");
                        try {
                            Thread.sleep(rand.nextInt(20));
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        System.out.println("that they are endowed by their Creator with certain unalienable Rights,");
                        try {
                            Thread.sleep(rand.nextInt(20));
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        System.out.println("that among these are Life, Liberty and the pursuit of Happiness.");
                        try {
                            Thread.sleep(rand.nextInt(20));
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        turn = 0;
                        flag[1]=false;




            }

        }

    }
    public static void main(String[] args) {

        Thread thread1 = new Thread(new thread1()) ;
        Thread thread2 = new Thread(new thread2()) ;
        thread1.start();
        thread2.start();
    }
}

预期的输出:每个线程应打印五行以下行而不进行交织: 1.我们认为这些真理是不言而喻的,即所有人都是平等的, 2.创造者赋予他们某些不可剥夺的权利, 3.其中包括生命,自由和对幸福的追求。

实际输出是仅通过一个线程将五行打印出的三行

0 个答案:

没有答案