当前位置:网站首页>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





边栏推荐
- Software testing assignment - the next day
- Software testing learning - day 3
- Split small interface
- 【最详细】最新最全Redis面试大全(50道)
- [HCAI] learning summary OSI model
- Advanced API (UDP connection & map set & collection set)
- Discussion on some problems of array
- instanceof
- Jmeter+influxdb+grafana of performance tools to create visual real-time monitoring of pressure measurement -- problem record
- Advanced API (multithreading 02)
猜你喜欢

Setting up the development environment of dataworks custom function

Final, override, polymorphism, abstraction, interface

Winter vacation work of software engineering practice

VMware virtual machine C disk expansion

Jenkins

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

Interfaces and related concepts

Arctic code vault contributor

Pits encountered in the use of El checkbox group

Integration test practice (1) theoretical basis
随机推荐
How can I split a string at the first occurrence of “-” (minus sign) into two $vars with PHP?
Shim and Polyfill in [concept collection]
La loi des 10 000 heures ne fait pas de vous un maître de programmation, mais au moins un bon point de départ
4279. Cartesian tree
VMware virtual machine C disk expansion
Book recommendation~
10 000 volumes - Guide de l'investisseur en valeur [l'éducation d'un investisseur en valeur]
[most detailed] latest and complete redis interview book (50)
Thoughts on project development
Sorting out the core ideas of the pyramid principle
Tool class static method calls @autowired injected service
Software testing assignment - the next day
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'?
Search engine Bing Bing advanced search skills
RestHighLevelClient获取某个索引的mapping
Getting started with pytest
Distributed ID
"Baidu Cup" CTF game 2017 February, Web: blast-1
High concurrency memory pool
Hands on redis master-slave replication, sentinel master-slave switching, cluster sharding