Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods that Java programming producers and consumers implement?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what are the methods for Java programming producers and consumers to achieve". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what methods Java programming producers and consumers achieve.

Catalogue

Four ways to realize producers and consumers

First, the most basic

II. Lock framework in java.util.concurrent.lock

Third, the implementation of blocking queue BlockingQueue

Some methods of Blockqueue Interface

Fourth, the realization of semaphore Semaphore

Four ways to realize producers and consumers: first, the most basic

Using the wait () and notify () methods, the wait () method is called when the buffer is full or empty, and all threads are awakened when the producer produces a product or the consumer consumes a product.

Package com.practice;public class testMain {private static Integer count = 0; private static final Integer FULL = 10; private static String LOCK = "lock"; public static void main (String [] args) {testMain testMain = new testMain (); new Thread (testMain.new Producer ()). Start (); new Thread (testMain.new Consumer ()). Start (); new Thread (testMain.new Producer ()). Start () New Thread (testMain.new Consumer ()). Start (); new Thread (testMain.new Producer ()). Start (); new Thread (testMain.new Consumer ()). Start (); new Thread (testMain.new Producer ()). Start (); new Thread (testMain.new Consumer ()). Start () } class Producer implements Runnable {@ Override public void run () {for (int I = 0; I < 10; iTunes +) {try {Thread.sleep (3000);} catch (Exception e) {e.printStackTrace () } synchronized (LOCK) {while (count = = FULL) {/ / cache space is full of try {LOCK.wait (); / / Thread blocking} catch (Exception e) {e.printStackTrace () }} count++;// producer System.out.println (Thread.currentThread (). GetName () + "producer production, currently a total of" + count); LOCK.notifyAll () / / Wake up all threads}} class Consumer implements Runnable {@ Override public void run () {for (int I = 0; I < 10; iTunes +) {try {Thread.sleep (3000) } catch (InterruptedException e) {e.printStackTrace ();} synchronized (LOCK) {while (count = = 0) {try {LOCK.wait () } catch (Exception e) {}} count--; System.out.println (Thread.currentThread (). GetName () + "Consumer consumption, total consumption at present" + count); LOCK.notifyAll () / / Wake up all threads}} 2. Lock framework in java.util.concurrent.lock

The display control of the lock is realized through the lock () method and the unlock () method of lock, while synchronize () is the invisible control of the lock, and the reentrant lock is also called the recursive lock, which means that after the outer function of the same thread acquires the lock, the inner recursive function still has the code to acquire the lock, but it is not affected.

To put it simply, the lock maintains a counter related to acquiring the lock. If a thread that owns the lock gets the lock again, the counter is increased by 1, and the function call end counter is subtracted by 1. Then the lock needs to be released twice before it can be really released, and the thread that has acquired the lock will not be blocked when entering other synchronous code blocks that need the same lock.

Import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;public class ReentrantLockTest {private static Integer count = 0; private static Integer FULL = 10; / / create a lock object private Lock lock = new ReentrantLock (); / / create two condition variables, one buffer is not full and the other buffer is not empty private final Condition notFull = lock.newCondition (); private final Condition notEmpty = lock.newCondition () Public static void main (String [] args) {ReentrantLockTest testMain = new ReentrantLockTest (); new Thread (testMain.new Producer ()). Start (); new Thread (testMain.new Consumer ()). Start (); new Thread (testMain.new Producer ()). Start (); new Thread (testMain.new Consumer ()). Start (); new Thread (testMain.new Producer ()). Start () New Thread (testMain.new Consumer ()). Start (); new Thread (testMain.new Producer ()). Start (); new Thread (testMain.new Consumer ()). Start ();} class Producer implements Runnable {@ Override public void run () {for (int I = 0; I)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report