Example of small Java Production and Consumption threads

Source: Internet
Author: User

The Production and Consumption thread type is a good example of understanding multithreading. In fact, it should be accurate to say that it is the "producer-consumer-warehouse" model. when it leaves the warehouse, the producer and consumer model becomes unconvincing.

For this model, the following points should be clarified:

1. The producer only produces when the warehouse is not full, and stops producing when the Warehouse is full.

2. Consumers can only consume data when there are products in the warehouse, while empty warehouses are waiting.

3. When a consumer finds that there is no product in the warehouse to be consumed, the producer will be notified of production.

4. When the producer generates a consumable product, it should notify the waiting consumer to consume it.

Code:

1. Production Thread class:

Code:
  1. Import java. util. List;
  2. Public class product implements runnable {
  3. Private list Container = NULL;
  4. Private int count;
  5. Public Product (list lst ){
  6. This. Container = lst;
  7. }
  8. Public void run (){
  9. While (true ){
  10. Synchronized (container ){
  11. If (container. Size ()> = multithread. max) {// if it exceeds the set Max inventory, the producer waits for wait
  12. Try {
  13. Container. Wait ();
  14. } Catch (interruptedexception e ){
  15. E. printstacktrace ();
  16. }
  17. }
  18. Try {
  19. Thread. Sleep (100 );
  20. } Catch (interruptedexception e ){
  21. E. printstacktrace ();
  22. }
  23. Container. Add (new object ());
  24. Container. Y (); // notify wake up other threads on the container (consumer)
  25. System. Out. println ("I have produced" + (++ count) + ");
  26. }
  27. }
  28. }
  29. }

--

2 consumption thread:

Code:
  1. Import java. util. List;
  2. Public class consume implements runnable {
  3. Private list Container = NULL;
  4. Private int count;
  5. Public consume (list lst ){
  6. This. Container = lst;
  7. }
  8. Public void run (){
  9. While (true ){
  10. Synchronized (container ){
  11. If (container. Size () = 0) {// if the container is empty, the consumer waits for wait (producer)
  12. Try {
  13. Container. Wait (); // discard the lock
  14. } Catch (interruptedexception e ){
  15. E. printstacktrace ();
  16. }
  17. }
  18. Try {
  19. Thread. sleep (100 );
  20. } Catch (InterruptedException e ){
  21. E. printStackTrace ();
  22. }
  23. Container. Remove (0 );
  24. Container. Y (); // wake up the producer
  25. System. Out. println ("I have eaten" + (++ count) + ");
  26. }
  27. }
  28. }
  29. }

--

3. Run the test:

Code:
  1. Import java. util. arraylist;
  2. Import java. util. List;
  3. Public class MultiThread {
  4. Private List container = new ArrayList ();
  5. Public final static int MAX = 3; // maximum inventory
  6. Public static void main (String args []) {
  7. MultiThread m = new MultiThread ();
  8. New Thread (new Consume (m. getContainer (). start ();
  9. New Thread (new Product (m. getContainer (). start ();
  10. }
  11. Public List getContainer (){
  12. Return container;
  13. }
  14. Public void setContainer (List container ){
  15. This. container = container;
  16. }
  17. }

In this example, when the production or consumption conditions are not met, the wait method of the object is called. The wait method is used to release the lock obtained by the current thread, and call the notify () method of the object to notify (wake up) Other waiting threads on the object so that it can continue execution. In this way, the entire producer and consumer thread can be correctly executed collaboratively.

The notify () method serves as a notification, neither releasing or obtaining a lock.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.