当前位置:网站首页>Interview questions about producers and consumers (important)
Interview questions about producers and consumers (important)
2022-07-03 07:10:00 【L gold p】
1、 Deadlock
1.1 summary

1.2 Code implementation



2、 Thread communication
2.1


2.2 Usage mode





2.3 Producer consumer

package day_02;
import java.util.Random;
/**
* Similar to printing odd and even numbers , Use wait and notifyAll
*
* 1 A business class SynStack There's a variable , Used to save the number of produced elements
* 2 There is one in the business class char Array , Elements used to hold production ( If only produce a-z These letters )
* 3 There need to be two methods in the business class , One is production push , A consumer pop
* push Method It is mainly used to add data to the array
* Number to +1 , Also determine whether it is full , When it's full, hang up and wait
* pop Method It is mainly used to fetch the data in the array
* Number to -1 , But also to determine whether the consumption is over , When it's over, hang up and wait
* 4 Two threads , One is responsible for the production of , One is responsible for consumption
*
* @author Dawn Education - Handsome and juicy, your brother Ze
* @Date 2022 year 1 month 25 Japan In the morning 11:14:56
*/
public class Thread_03_ProduceConsumer {
public static void main(String[] args) {
SynStack ss = new SynStack();
Thread p = new Thread(new Producer(ss));
Thread c = new Thread(new Consumer(ss));
p.start();
c.start();
}
}
class Producer implements Runnable{
SynStack ss ;
public Producer(SynStack ss) {
super();
this.ss = ss;
}
@Override
public void run() {
Random random = new Random();
while (true) {
char ch = (char) (random.nextInt(26)+97);
ss.push(ch);
}
}
}
class Consumer implements Runnable{
SynStack ss ;
public Consumer(SynStack ss) {
super();
this.ss = ss;
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
ss.pop();
}
}
}
class SynStack{
// Container for saving data
char[] data = new char[6];
// Number of production
int count = 0;
// production
public synchronized void push(char ch) {
// Judge whether it is full
if (count == data.length) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// That means it's not full , Started to produce
// Awaken consumers to prepare for consumption
this.notifyAll();
data[count ] = ch;
count++;
System.out.println(" Produced "+ch+" , The remaining "+count+" Elements ");
}
public synchronized char pop(){
// Determine whether it is null
if (count == 0) {
try {
// There's no need to wake up the producers here , Because the producer is full in wait, It's all empty , It means that the producer must not wait
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// This is not empty , Start spending
count--;
char ch = data[count];
// Wake up producers
this.notifyAll();
System.out.println(" The consumption "+ch+" , The remaining "+count+" Elements ");
return ch;
}
}
3. The singleton pattern





边栏推荐
- How to specify the execution order for multiple global exception handling classes
- JUC forkjoinpool branch merge framework - work theft
- Resthighlevelclient gets the mapping of an index
- Software testing learning - the next day
- MySQL transaction rollback, error points record
- 【已解决】SQLException: Invalid value for getInt() - ‘田鹏‘
- Realize PDF to picture conversion with C #
- Summary of abnormal mechanism of interview
- Modify MySQL password
- Advanced API (use of file class)
猜你喜欢

在 4EVERLAND 上存储 WordPress 媒体内容,完成去中心化存储

Reading notes of "learn to ask questions"

JUC forkjoinpool branch merge framework - work theft

dataworks自定義函數開發環境搭建

How to specify the execution order for multiple global exception handling classes

The pressure of large institutions in the bear market has doubled. Will the giant whales such as gray scale, tether and micro strategy become 'giant thunder'?

Interfaces and related concepts

Practice of enterprise ab/testing platform

dataworks自定义函数开发环境搭建

4279. 笛卡尔树
随机推荐
Error c2017: illegal escape sequence
centos php7.2.24升级到php7.3
The pressure of large institutions in the bear market has doubled. Will the giant whales such as gray scale, tether and micro strategy become 'giant thunder'?
Advanced API (use of file class)
[Code] occasionally take values, judge blanks, look up tables, verify, etc
Sorting, dichotomy
Abstract learning
Realize PDF to picture conversion with C #
Getting started with pytest
Book recommendation~
[Code] if (list! = null & list. Size() > 0) optimization, set empty judgment implementation method
691. Cube IV
How can the server set up multiple interfaces and install IIS? Tiantian gives you the answer!
The 10000 hour rule won't make you a master programmer, but at least it's a good starting point
Upgrade CentOS php7.2.24 to php7.3
RestHighLevelClient获取某个索引的mapping
Practical plug-ins in idea
My 2020 summary "don't love the past, indulge in moving forward"
Use of framework
4EVERLAND:IPFS 上的 Web3 开发者中心,部署了超过 30,000 个 Dapp!