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





边栏推荐
猜你喜欢

Specified interval inversion in the linked list

Integration test practice (1) theoretical basis

Pits encountered in the use of El checkbox group

Reading notes of "learn to ask questions"

Realize PDF to picture conversion with C #

Winter vacation work of software engineering practice
![[classes and objects] explain classes and objects in simple terms](/img/41/250457530880dfe3728432c2ccd50b.png)
[classes and objects] explain classes and objects in simple terms

Jenkins

Recursion, Fibonacci sequence
![[set theory] partition (partition | partition example | partition and equivalence relationship)](/img/f0/c3c82de52d563f3b81d731ba74e3a2.jpg)
[set theory] partition (partition | partition example | partition and equivalence relationship)
随机推荐
Integration test practice (1) theoretical basis
How can the server set up multiple interfaces and install IIS? Tiantian gives you the answer!
【已解决】SQLException: Invalid value for getInt() - ‘田鹏‘
How to migrate or replicate VMware virtual machine systems
Notes on the core knowledge of Domain Driven Design DDD
Laravel Web框架
Dbnet: real time scene text detection with differentiable binarization
MySQL mistakenly deleted the root account and failed to log in
Realize PDF to picture conversion with C #
【CMake】CMake链接SQLite库
[Code] if (list! = null & list. Size() > 0) optimization, set empty judgment implementation method
Advanced API (use of file class)
Split small interface
萬卷書 - 價值投資者指南 [The Education of a Value Investor]
Hands on redis master-slave replication, sentinel master-slave switching, cluster sharding
JMeter test result output
Software testing learning - day one
Book recommendation~
Selenium key knowledge explanation
Shim and Polyfill in [concept collection]