简体   繁体   English

为什么我的Java多线程代码无法正常工作?

[英]Why my Java multithreading code does not work properly?

Really need your help to figure out what is going on. 确实需要您的帮助才能弄清楚发生了什么。 The code seem to be so trivial, but it produces wrong output. 该代码看似微不足道,但会产生错误的输出。

It is a basic producer-consumer problem. 这是一个基本的生产者-消费者问题。 Generator thread generates prime numbers and consumer (acceptors) must process them. 生成器线程生成质数,并且使用者(acceptors)必须对其进行处理。 In App I created two acceptors and 1 generator thread. 在App中,我创建了两个接受器和1个生成器线程。 The problem is that I get the following output for 50 as a bound: 问题是我得到以下输出为50:

Thread Thread-2 puts 47
Thread Thread-2 puts 43
Thread Rob gets 47
Thread Rob gets 43
Thread Thread-1 gets 47
Thread Nick puts 47
etc...

I have no idea why Thread-2 and Thread-1 are printed... where these threads are coming from? 我不知道为什么要打印Thread-2和Thread-1 ...这些线程来自哪里? Thanks 谢谢

public class PrimeGenerator implements Runnable{
    private PrimeCentral primeCentral;
    int bound;

    public PrimeGenerator(PrimeCentral PC, int bound){
        this.primeCentral = PC;
        this.bound = bound;
        new Thread(this).start();
    }

    @Override
    public void run() {
          int n=bound;
          while (n > 1) {
             int d;
             for (d=2; d <= n/2; d++) 
                if ((n % d) == 0) break; 
             if (d > n/2) {
                 primeCentral.put(n);
                System.out.println("Thread " + Thread.currentThread().getName() + " puts " + n);
             }
          n--; 
          }

    }

}

public class PrimeCentral {
    private int prime;
    private boolean available = false;


    public synchronized void put(int n){
        while(available == true){
            try{
                wait();
            }catch(InterruptedException e){}
        }
        prime = n;
        available = true;
        notifyAll();        
    }

    public synchronized int get(){
        while(available == false){
            try{
                wait();
            }catch(InterruptedException e){}
        }
        available = false;
        notifyAll();
        return prime;
    }

}
public class PrimeAcceptor implements Runnable{
    private PrimeCentral primeCentral;

    public PrimeAcceptor(PrimeCentral primeCentral){
        this.primeCentral = primeCentral;
        new Thread(this).start();
    }


    public void run() {
          while (true) {
              int prime = primeCentral.get();
              System.out.println("Thread " + Thread.currentThread().getName() + " gets " + prime);
              if (prime == 2) break; 
          }
    }

public class App {

    public static void main(String[] args) {

        PrimeCentral pc = new PrimeCentral();

        new Thread(new PrimeAcceptor(pc), "Bob").start();
        new Thread(new PrimeAcceptor(pc), "Rob").start();

        new Thread(new PrimeGenerator(pc, 50), "Nick").start();

    }
}

EDIT: sorry people I was dumb with this stupid mistake 编辑:对不起,我被这个愚蠢的错误愚弄的人

You're starting two threads with new Thread(this).start(). 您正在使用new Thread(this).start().启动两个线程new Thread(this).start().

They will both have names of the form you mention. 它们都将具有您提到的表单的名称。

I think you're starting four threads where you only meant to start two. 我认为您正在启动四个线程,而您只打算启动两个线程。 Delete the above line in both places it occurs. 在出现的两个地方都删除上面的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM