奇数甚至序列java 2线程

时间:2015-06-05 04:20:10

标签: java multithreading

我正在尝试编写一个包含两个Java线程的程序。一个应打印奇数,另一个打印偶数。输出应按顺序排列。我的代码无法正常工作。请更正并告诉我错误是什么。

 xAxis: {
        gridLineWidth: 0,   
    },

获得输出

  

0,1

3 个答案:

答案 0 :(得分:3)

这是一种更清晰的方式来实现你想要的,没有代码中的丑陋睡眠,更不用说它会比睡眠中的代码运行得更快,原因显而易见。

public class App {

    public static void main(String[] args) {
        PrintCl pcl = new PrintCl();

        Thread t1 = new Thread(new ThrdEven(pcl));
        Thread t2 = new Thread(new ThrdOdd(pcl));

        t1.start();
        t2.start();
    }
}

public class ThrdEven implements Runnable {

    private PrintCl pcl = null;

    public ThrdEven(PrintCl pcl) {
        this.pcl = pcl;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i += 2) {
            pcl.Even(i);
        }
    }
}

public class ThrdOdd implements Runnable {

    private PrintCl pcl = null;

    public ThrdOdd(PrintCl pcl) {
        this.pcl = pcl;
    }

    @Override
    public void run() {
        for (int i = 1; i < 10; i += 2) {
            pcl.odd(i);
        }
    }
}

public class PrintCl {

    private final Object _lock = new Object();
    private boolean isEvenAllowed = true;

    public void Even(int n) {
        synchronized (this._lock) {
            while (!this.isEvenAllowed) {
                try {
                    this._lock.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    throw new RuntimeException(e);
                }
            }
            System.out.println(n);
            this.isEvenAllowed = false;
            this._lock.notifyAll();
        }
    }

    public void odd(int n) {
        synchronized (this._lock) {
            while (this.isEvenAllowed) {
                try {
                    this._lock.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    throw new RuntimeException(e);
                }
            }
            System.out.println(n);
            this.isEvenAllowed = true;
            this._lock.notifyAll();
        }
    }
}

答案 1 :(得分:2)

请尝试在代码中进行以下更改:

public class App {

  public static void main(String[] args) throws InterruptedException {

  PrintCl pcl =new PrintCl();

  ThrdO to=new ThrdO();
  to.setPcl(pcl);
  Thread t1=new Thread(to);

  ThredE te=new ThredE();
  te.setPcl(pcl);
  Thread t2=new Thread(te);
  t1.start();
  Thread.sleep(1000);
  t2.start();

 }
}

对于Thrd0:

public class ThrdO implements Runnable  {

  PrintCl pcl =null;

  @Override
  public void run() {
    for(int i=0;i<10;i+=2)
    pcl.Even(i);

  }

  public PrintCl getPcl() {
    return pcl;
  }

  public void setPcl(PrintCl pcl) {
    this.pcl = pcl;
  }


}

ThredE:

public class ThredE implements Runnable {

  PrintCl pcl =null;

  @Override
  public void run() {
    for(int i=1;i<10;i+=2)
        try {
            pcl.odd(i);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

  }

  public PrintCl getPcl() {
    return pcl;
  }

  public void setPcl(PrintCl pcl) {
    this.pcl = pcl;
  }
}

答案 2 :(得分:1)

您的代码有两个基本问题

  1. 每个帖子都有自己的.directive("myDir", function(){ return { restrict: 'E', scope:{ showDiv: '=' }, link:function($scope,element){ $scope.showDiv = true; //div will shown when directive is present $scope.$on('$destroy', function(){ $scope.showDiv = false; //div will get hidden when directive is removed or not present }); } } }) 。因此,一旦打印出他们的第一个号码,他们就会无休止地等待通知。
  2. 一旦你解决了这个问题,另一个问题是你的一个线程将完成,但第二个线程仍然在等待它的通知,它永远不会死。
  3. 我已修复以下代码中的两个问题

    printing resource