Communication between java threads

-----------------------------------Communication between java threads ---------- --------------------------

 

 

producer and consumer

 


The producer produces fruit. If the fruit is not bought, it will not be produced. If the fruit is bought by the consumer, the consumer will notify the producer and tell him that we have bought the fruit and ask for production.

 

The same is true for consumers. If the fruit has been produced, then buy it. After buying it, notify the producer that the fruit is no longer available and ask for production.

 

Notice:

1. Communication and shared data between threads must have synchronized code blocks synchronized

       2. There must be wait and notify, and the two must appear in pairs.

       3. The thread implementation of producers and consumers must be in the while(true) infinite loop .

 

 

Example:

 

fruit

 

public class Fruit {

 

    private String name;

   

    private  boolean  isExist;

 

    public String getName() {

        return  name;

    }

 

    public  void setName(String name) {

        this.name = name;

    }

 

    public boolean isExist() {

        return  isExist;

    }

 

    publicvoid setExist(booleanisExist) {

        this.isExist = isExist;

    }

}

 

 

producer class

 

public  class ProductFruit implements Runnable {

 

    private Fruit fruit;

   

    public ProductFruit(Fruit fruit) {

        super();

        this.fruit = fruit;

    }

 

    @Override

    public  void run() {

       

        while(true) {

           // There is shared data, multiple threads operate shared data, and locks must be used

           synchronized (fruit) {

               // If the fruit already exists, the producer will not produce it, waiting for the consumer to buy the fruit and reproduce it

               if(fruit.isExist()) {

                   try {

                       // The currently produced fruit is suspended and becomes blocked

                       fruit.wait();

                   } catch (InterruptedException e) {

                       e .printStackTrace ();

                   }

               }

               try {

                   Thread.sleep(100);

               } catch (InterruptedException e) {

                   e .printStackTrace ();

               }

               System.out .println ( fruit .getName () + " Fruit is produced " );

               // Change the state of the fruit to exist

               fruit.setExist(true);

               // Wake up the thread waiting to buy fruit

               fruit.notify();

              

           }

        }

    }

}

 

buyer class

public  class BuyFruit implements Runnable{

 

    private Fruit fruit;

   

    public BuyFruit(Fruit fruit) {

        super();

        this.fruit = fruit;

    }

 

    @Override

    public  void run() {

       

        while(true) {

           // There is shared data, multiple threads operate shared data, and locks must be used

           synchronized (fruit) {

               if(!fruit.isExist()) {

                   try {

                       fruit.wait();

                   } catch (InterruptedException e) {

                       e .printStackTrace ();

                   }

               }

               try {

                   Thread.sleep(100);

               } catch (InterruptedException e) {

                   e .printStackTrace ();

               }

               System.out .println ( fruit .getName () + " Fruit was bought " );

               // Set the fruit state to false

               fruit.setExist(false);

               // wake up the producer to produce fruit

               fruit.notify();

           }

        }  

    }   

}

 

Client class

 

public   class Client {

 

    public  static  void main(String[] args) {

       

        // create fruit object

        Fruit f = new Fruit();

        f .setName( " apple " );

        f.setExist(false);

       

        // create producer

        ProductFruit pf = new ProductFruit(f);

        // create consumer

        BuyFruit bf = new BuyFruit(f);

       

        // create thread

        Thread t1 = new Thread(pf);

        Thread t2 = new Thread(bf);

       

        // start the thread

        t1.start();

        t2.start();

       

/*      The apple fruit is produced

        Apple fruit was bought

        apple fruit is produced

        Apple fruit was bought

        apple fruit is produced

        Apple fruit was bought

        Apple fruit is produced */

    }

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324605149&siteId=291194637
Recommended