Producer consumer issues, Java implementations

Source: Internet
Author: User

Come on, try to fix this problem today. Or this rhythm, look at a question to first from the history, the overall view, so that we can really grasp the whole picture, the final conquer, clear in the chest!

Let's start by reviewing the following concepts:

1. Basic Conceptsof basic
    1. Programs-Program
      A program is a static source code or a target program, is a lifeless entity.

    2. Processes-Process
      When the CPU gives the program life, that is, when the operating system executes it, the program becomes an active entity (but not an executable entity), called a process-in-progress program.

      • A process is an instance of a program;
      • is the basic unit of computer resource allocation;
      • is a container for threads;
    3. Threads-Thread
      A thread is an executable entity in a process, is a system-independent dispatch and a basic unit, and the thread itself does not own system resources.

      • Threads are an important indicator of modern operating systems.
      • All threads in a process share all the resources of the process.
Step-by-step

1. interprocess Communication-IPC

interprocess communication-inter-process communication, IPC;
The IPC is a standard UNIX communication mechanism, a set of programming interfaces that allows programmers to coordinate different processes so that they can run concurrently in one OS and communicate and exchange information with each other.

There are two types of IPC methods initially:

    1. Signal-Signals
      is one of the earliest IPC methods used in UNIX systems.
      A series of events (signal sources), such as a keyboard interrupt or an error, are pre-defined in the OS to produce a signal. The OS notifies the current process system of a certain pre-defined event that occurs through a signal.
      When the process recognizes the arrival of the signal, it takes appropriate action to transmit or process the signal.

    2. Pipes- Pipe
      The table is temporarily not.

Later, for the first time in System V Unix (1983, Orthodox Unix), 3 of the IPC mechanisms were introduced:

    1. Messages Queue-message queues
      Message Queuing is a linked list of messages that allows one or more processes to write messages to it, and one or more processes to read messages from them.
      Linux maintains an MQ vector table: msgque represents Message Queuing.

    2. Semaphore /semaphore-Semaphore
      A semaphore is essentially a counter used to record the access status of a resource. There is a mutually exclusive semaphore, the condition signal quantity.

    3. Shared Memory
      Shared memory is typically created by a process, and the rest of the process reads and writes the memory. You typically use semaphores to manage shared memory.

In addition, there is a very important way:

    1. Socket- socket
      TCP/IP is a network communication protocol, and the socket is an implementation API for these protocols, which is implemented in BSD Unix first. Today sockets are the most common network programming API, and SOCKETAPI are available in all OS that provide the TCP/IP protocol stack.

2. How do I communicate between threads?

The process can communicate through so many IPC means, so what about threads?

In fact, the communication between the threads is very simple, because the threads of the same process share the resources of the process, so the communication between them is actually to read and write the shared resources . Of course, mutually exclusive means may be required to ensure data consistency.

Tips

Now I think you can understand the relevant knowledge points of Java thread synchronization.
A "shared resource", such as a member variable in a class, that is referred to as thread synchronization, which is actually shared by threads in the heap or in the method area in memory in the JVM runtime.
Again, this area is the resource area of the JVM process, the shared resource that all JVM threads can read and write.

Thread synchronization is a means of securing thread-safe access to shared resources, including:

    • Events-Event
    • Mutex-Mutex
    • Semaphore/semaphore-Semaphore
    • Critical resources
2. Producer Consumer issues 1. Throw a question

Producer Consumer issues (Producer-consumer problem) can also be called limited buffering problems (Bounded-buffer problem). This problem is a very classic (simple) process/thread synchronization problem.

The problem is as follows:

First, there is a product buffer in the factory, and the producer keeps adding new products to the buffer, while the consumer keeps taking the product away from the buffer.

Problem:
How do you ensure that the producer does not add data when the buffer is full, and the consumer does not consume the data when the buffer is empty?

The answer is also simple:

The producer must hibernate when the buffer is full, until the consumer consumes the product to be awakened.
Also, consumers must be allowed to hibernate when the buffer is empty, until the producer adds a product to the buffer to wake the consumer.

For the program design level, the solution is the IPC method mentioned above 1, the most commonly used is the semaphore method , so we come to specifically say the semaphore.

2. Signal Volume-semaphore

1. Overview
As mentioned above, the semaphore is essentially a counter that can be used to handle process synchronization problems.

In 1965, Dutch computer scientist Edsger Dijkstra (Edsger W. Dijkstra) invented the semaphore mechanism, which is now widely used in various OS.

2. Description in OS:
In the system, each process is given a semaphore, which represents the current state of each process, and the uncontrolled process is forced to stop at a specific place, waiting for the signal to continue to come.

3. Type:

    1. Semaphore is an arbitrary integer
      Known as: Count Semaphore (counting semaphore) or general signal volume (generic semaphore)

    2. The semaphore is only 0 or 1 of the binary
      Called: binary semaphore (binary semaphore) or mutually exclusive semaphore-mutex.

4. PV Primitives:
Dijkstra also proposes two primitives (atomic statements) to manipulate the semaphore:

    1. P Primitive Language
      P is the first letter of the Dutch language Proeren (test).
      P is the blocking primitive, which is responsible for converting the current process from a running state to a blocking state until another process wakes it up.

      The action represented is: request an idle resource (semaphore-1), exit if successful, or block the process if it fails;

    2. V Primitives
      V is the first letter of the Verhogen (added) of the Dutch language.
      V is the wake-up primitive, responsible for waking up a blocked process, which has a parameter table that records the process waiting to be awakened.

      The action represented is to release an occupied resource (semaphore + 1) and select one to wake up if a blocked thread is found.

5. Three types of use:
3 of the two species and 4 of the PV primitive combination, can be used to semaphore operation can be divided into three cases:

    1. The semaphore is a lock flag bit that implements mutually exclusive access to a shared variable.
      Process:

      // mutex的初始值为1,访问该共享数据;V(mutex);//非临界区
    2. Depending on the number of shared resources remaining, Semaphore provides access to a class-shared resource.
      Process:

      // mutex的初始值为资源的个数N,使用该资源;V(mutex);//非临界区
    3. View semaphore as a synchronization tool between processes.
      Process:

      临界区C1;P(S); V(S);临界区C2;
3. Implementation in Java

Producer consumer issues are specific to the following types of

    • 1 Producers 1 Consumers
    • 1 Producers of N consumers
    • M Producers N Consumers

I feel that most of the people on the Internet are writing the first kind of teaching, and I have written a third one according to my understanding, in fact I do not know right ah ... Because I didn't find what I thought was right, I could see ...

Readers, if there are inappropriate, but also hope to be generous to enlighten:

ImportJava.util.LinkedList;ImportJava.util.Random;/** * Multi-producer, multi-consumer situation * * @author alanzhangyx * * * * Public  class producerconsumer {    //define a queue buffer with the data as an integerlinkedlist<integer> list =NewLinkedlist<integer> ();//Set buffer maximum capacity    Static Final intMax_size = -;/** * producers. * * <p> producer v Primitive Operation </p> * <ul> * <li> get buffer, if buffer does not reach max_size, then produce one product (n rows) into buffer and wake all threads </li> * <li> Anyway finally put yourself to sleep (here is wait) </li> * </ul> * * @version 1.0.0 *
     
       @author Alanzhangyx *
      /Class Producer implements runnable{@Override         Public void Run() { while(true) {synchronized(list) {if(List.size () < max_size) {intnum =NewRandom (). Nextint ( -);                        List.add (num);                        List.notifyall (); System.out.println ("producer"+ Thread.CurrentThread (). GetName () +"Production of Products:"+ num +",----the buffer capacity at this time is"+ list.size ()); }Try{list.wait (); }Catch(Interruptedexception e)                    {E.printstacktrace (); }                }            }        }    }/** * consumers. * * <p> consumer for P primitive operation </p> * <ul> * <li> get buffer, if buffer has data, remove one product from buffer (n rows) and wake all threads </li>     ;   * <li> finally put myself to sleep (here is wait) </li> * </ul> * * @version 1.0.0 * @author Alanzhangyx * *Class Consumer implements runnable{@Override         Public void Run() { while(true) {synchronized(list) {if(List.size () >0) {intnum = List.poll ();//poll is the operation of the queue, deleting the header elementSystem.out.println ("Consumer"+ Thread.CurrentThread (). GetName () +"Consumption of Products:"+ num +",----the buffer capacity at this time is"+ list.size ());                    List.notifyall (); }Try{list.wait (); }Catch(Interruptedexception e)                    {E.printstacktrace (); }                }            }        }    } Public Static void Main(string[] args) {Producerconsumer PC =NewProducerconsumer ();The //thread constructor requires a Runnable object to construct a new thread, and the Runnable object can be reused without new multiple        //A consumer, a producerConsumer C = pc.NewConsumer (); Producer p = pc.NewProducer ();//producers and consumers who start all the same        NewThread (c). Start ();NewThread (c). Start ();NewThread (c). Start ();NewThread (c). Start ();NewThread (c). Start ();NewThread (P). Start ();NewThread (P). Start ();NewThread (P). Start ();NewThread (P). Start ();NewThread (P). Start (); }}
Conclusion

I don't know, do you understand? In fact, I summed up these just to understand the written on, there is always a kind of not so coherent and transparent feeling, but also because in fact, some places I have not been very thorough understanding.

The description of the related issues on the Internet is also vague, I am very upset

No matter, some things still have to go through a period of time to find new ideas, for the time being, a new understanding will immediately fill up.

Producer consumer issues, Java implementations

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.