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 is the relationship between consumers and producers in Java multithreading

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

Share

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

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)

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