当前位置:网站首页>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
边栏推荐
- The essence of interview
- [solved] unknown error 1146
- RestHighLevelClient获取某个索引的mapping
- Sorting out the core ideas of the pyramid principle
- 2022-06-23 vgmp OSPF inter domain security policy NAT policy (under update)
- 【CMake】CMake链接SQLite库
- JMeter JSON extractor extracts two parameters at the same time
- [set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*
- Software testing assignment - day 3
- 【无标题】
猜你喜欢
Gridome + strapi + vercel + PM2 deployment case of [static site (3)]
Selenium key knowledge explanation
Jenkins
[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*
Software testing learning - the next day
Software testing learning - day 3
EasyExcel
Mise en place d'un environnement de développement de fonctions personnalisées
691. 立方体IV
10000小時定律不會讓你成為編程大師,但至少是個好的起點
随机推荐
Software testing assignment - the next day
[day15] introduce the features, advantages and disadvantages of promise, and how to implement it internally. Implement promise by hand
Software testing assignment - day 1
Interface learning
Deep learning parameter initialization (I) Xavier initialization with code
Advanced API (UDP connection & map set & collection set)
Resthighlevelclient gets the mapping of an index
POI excel percentage
2022 - 06 - 23 vgmp - OSPF - Inter - Domain Security Policy - nat Policy (Update)
Specified interval inversion in the linked list
Arctic code vault contributor
Basic components and intermediate components
Practical plug-ins in idea
Inno setup production and installation package
Modify MySQL password
Advanced API (local simulation download file)
MySQL syntax (basic)
Inno Setup 制作安装包
【CMake】CMake链接SQLite库
SecureCRT取消Session记录的密码