java - 多线程生产者消费者问题,出现了空值,问题出在哪里?
大家讲道理
大家讲道理 2017-04-17 17:58:37
0
2
480

多线程生产者消费者问题, producer生产电影(两种), customer 观看电影


Film.java

package Film;

public class Film {
    private String name;
    private String actor;
    private boolean flag = false;

    
    
    public synchronized void set(String name, String actor)
            throws InterruptedException {
        
        if(!flag){
            this.wait();
        }

        this.setName(name);
        Thread.sleep(1000);
        this.setActor(actor);

        flag = false;
        this.notify();
    }
    
    public synchronized void get() throws InterruptedException{
        
        if(flag){
            this.wait();
        }
        Thread.sleep(1000);
        System.out.println("3"+this.getName()+"-->"+this.getActor());
        flag=true;
        this.notify();
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setActor(String actor) {
        this.actor = actor;
    }

    public String getName() {
        return name;
    }

    public String getActor() {
        return actor;
    }
}

Producer.java

package Film;

public class Producer implements Runnable {

    private Film film ;

    public Producer(Film film) {
        super();
        this.film = film;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub

        for (int i = 1; i < 10; i++) {

            if (0==i%2) {
                try {
                    System.out.println("1-produce film " + i);

                    film.set("film1", "actor1");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    System.out.println("1-produce film " + i);
                    film.set("film2", "actor2");

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

        }

    }

}

Customer.java


public class Customer implements Runnable {
    private Film film ;
    
    public Customer(Film film){
        this.film=film;
    
    }
    public void run() {
        
        for(int x=1;x<10;x++){
            try {
                Thread.sleep(1000);
                film.get();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }

    }

}

MainTest.java

package Film;

public class MainTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Film film = new Film();
        Producer pro = new Producer(film);
        Customer cus = new Customer(film);
        new Thread(pro).start();
        new Thread(cus).start();

    }

}

输出如下

1-produce film 1
3null-->null
1-produce film 2
3film2-->actor2
1-produce film 3
3film1-->actor1
1-produce film 4
3film2-->actor2
1-produce film 5
3film1-->actor1
1-produce film 6
3film2-->actor2
1-produce film 7
3film1-->actor1
1-produce film 8
3film2-->actor2
1-produce film 9
3film1-->actor1

请问第二行为什么会出现空值?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

répondre à tous(2)
洪涛

Dans le code Film, flag est initialisé à false.
Regardez les méthodes set et get dans votre classe Film. Vous devez exécuter get avant de pouvoir exécuter set.

Je pense donc que le drapeau au début des méthodes get et set dans la classe File devrait être écrit à l'envers !

PHPzhong
   private boolean flag = true;
public synchronized void set(String name, String actor)
            throws InterruptedException {
        
       
        this.setName(name);
        Thread.sleep(1000);
        this.setActor(actor);

        flag = false;
        this.notify();
    }
    
    public synchronized void get() throws InterruptedException{
        
        if(flag){
            this.wait();
        }
        Thread.sleep(1000);
        System.out.println("3"+this.getName()+"-->"+this.getActor());
        flag=true;
        this.notify();
    }
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal