Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the relationship between consumers and producers in Java multithreading

Shulou Source: shulou.com Published: 2022-06-02 12:54:10 10月01日 Update

This article will explain in detail about the relationship between consumers and producers in Java multithreading. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Multithreading: various tasks in CPU are executed alternately, which is called multithreading. Among them, a dynamic execution of each task is called a process. Pauses during process execution are called interrupts. The process is broken down into segments through interruptions, each of which is called a thread.

Among consumers and producers, we set that each activity of a single producer is steamed bread + 1; a single consumer carries out steamed bread-1 at a time; then the time of activity of consumers and producers is uncertain, and the number of producers and consumers is uncertain at the same time. We use multi-threading to implement this classic problem:

We first determine the physical object, that is, the goods generated by the producer each time, here we use steamed bread instead. In the WoTou class, we can see the id of steamed bread, which has its own unique attribute. We can clearly see the production and consumption process of steamed bread on the console. The specific implementation code is as follows:

Class WoTou {/ / steamed bread, steamed bread produced by producers

Int id

WoTou (int id) {

This.id = id

}

Public String toString () {

Return "WoTou:" + id

}

}

We need to determine the storage container of steamed bread, that is, where we need to judge when consumption and production occurs, whether the steamed bread is exhausted or whether the steamed bread is overflowing. Each activity is equivalent to picking up and putting things in the stack, so according to the characteristics of the stack: the first-in-first-out principle.

Class SyncStack {/ / basket container for steamed bread

Int index = 0

WoTou [] arrWT = new WoTou [6]

}

In the basket container, we simply define the maximum capacity of the stack as 6, and then we set the producer class, which is defined as the producer class. We know that producer has some unique attributes, that is, putting WoTou into the basket SyncStack, performing steamed bread + 1 operation each time, and the producer's operation Time interval, whether the interval is 10 milliseconds or 5 milliseconds. We make the producer class implement its Runnerable interface and call the thread's run () method. The specific code is as follows:

Class Producer implements Runnable {/ / producer

SyncStack ss= null

Producer (SyncStack ss) {

This.ss = ss

}

Public void run () {

For (int iTuno Bandi)

Tags: Production steamed bread consumption producer basket thread consumer quantity method code process at the same time container time activity release action capacity situation article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Docker MySQL NVidia Shulou Tech Info