当前位置:网站首页>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
边栏推荐
- 高并发内存池
- php artisan
- PHP install the spool extension
- Sorting out the core ideas of the pyramid principle
- Tool class static method calls @autowired injected service
- Software testing assignment - the next day
- 691. 立方体IV
- 2022 East China Normal University postgraduate entrance examination machine test questions - detailed solution
- Redis command
- Jmeter+influxdb+grafana of performance tools to create visual real-time monitoring of pressure measurement -- problem record
猜你喜欢
Software testing learning - the next day
691. Cube IV
Selenium key knowledge explanation
[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*
4279. 笛卡尔树
Win 10 find the port and close the port
Deep learning parameter initialization (I) Xavier initialization with code
【已解决】SQLException: Invalid value for getInt() - ‘田鹏‘
Practical plug-ins in idea
EasyExcel
随机推荐
Search engine Bing Bing advanced search skills
Centos切换安装mysql5.7和mysql8.0
[most detailed] latest and complete redis interview book (50)
IC_ EDA_ All virtual machine (rich Edition): questasim, vivado, VCs, Verdi, DC, Pt, spyglass, icc2, synthesize, innovative, ic617, mmsim, process library
How can the server set up multiple interfaces and install IIS? Tiantian gives you the answer!
2022-06-23 VGMP-OSPF-域间安全策略-NAT策略(更新中)
在 4EVERLAND 上存储 WordPress 媒体内容,完成去中心化存储
[Code] if (list! = null & list. Size() > 0) optimization, set empty judgment implementation method
Software testing learning - day one
Advanced API (serialization & deserialization)
Distributed ID
Class and object summary
File links cannot be opened or downloaded in Google browser
Thoughts on project development
Inno Setup 制作安装包
Selenium key knowledge explanation
Jmeter+influxdb+grafana of performance tools to create visual real-time monitoring of pressure measurement -- problem record
Software testing assignment - the next day
Interface learning
When MySQL inserts Chinese into the database, there is a diamond question mark garbled code