当前位置:网站首页>Thread - learning notes
Thread - learning notes
2022-06-25 15:27:00 【BlackPenguin】
Thread implementation
- Inherit extends Thread class
- Realization Runnable Interface
- Realization Callable Interface
- Thread pool : Is a thread queue , There are many threads stored inside , Can reduce the risk of creating 、 Time spent destroying threads
java Don't inherit more , But you can implement multiple interfaces , So use Runnable Interfaces are better than inheritance Thread Classes are more flexible , A class object implements multiple Runnable Interface .
producer / Consumer model - Thread communication
Include producers 、 consumer 、 And a buffer . producer -> The production data -> buffer -> Take the data -> consumer
The producer does not directly send the production data to consumers for use , Instead, the data is stored in the buffer first . Consumer fetches data from buffer .
advantage :
- There is little coupling between producers and consumers , It is a decoupling process
- Support concurrency , When the producer is faster , You don't have to wait for the consumer to finish processing the data , And waste time .
- It serves as a cache , The consumer didn't handle it in a timely manner , You can store unprocessed data in a buffer for later use
public class PCTest {
public static void main(String[] args) {
SynContainer synContainer = new SynContainer();
Producer producer = new Producer(synContainer);
Consumer consumer = new Consumer(synContainer);
Consumer consumer1 = new Consumer(synContainer);
new Thread(producer).start();
new Thread(consumer).start();
new Thread(consumer1).start();
}
}
// producer
class Producer implements Runnable {
SynContainer synContainer;
public Producer(SynContainer synContainer) {
this.synContainer = synContainer;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
synContainer.push(new Chicken(i));
}
}
}
// consumer
class Consumer implements Runnable {
SynContainer synContainer;
public Consumer(SynContainer synContainer) {
this.synContainer = synContainer;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
synContainer.pop();
}
}
}
// product
class Chicken {
int id;
public Chicken(int id) {
this.id = id;
}
}
// buffer
class SynContainer {
// You need a container
Chicken[] chickens = new Chicken[10];
// Container counter
int count = 0;
// Producers put in products
public synchronized void push(Chicken chicken) {
if(count == chickens.length) {
// Notify consumers , Producer waiting
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
chickens[count] = chicken;
System.out.println(" production id by " + chicken.id + ", count by " + count);
count++;
this.notify();
}
// Consumers consume products
public synchronized Chicken pop() {
while(count == 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count--;
Chicken chicken = chickens[count];
System.out.println(" The consumption id by " + chicken.id + ", count by " + count);
this.notify();
return chicken;
}
}
sleep And wait The difference between
sleep()Belong toThread;wait()Belong to Class, yes Object Class method ,Object.wait()sleep()A time will be passed in , When the time is up, it will return to the ready state ;wait()No incoming time , Get into “ Waiting indefinitely ”, Only passnotify()Wake up the .sleepThe current thread is asleep , however No release lock , Other threads cannot use locked resources , Sleep under the lock . Because the thread sleeps CPU Free , So it Release CPU Executive power ;waitRelease the lock , It can be understood as leaving the queue , Wait to be awakened before entering , Let others use resources first , also Release CPU Executive power .sleepCan be used anywhere ;waitCan only be used in synchronous methods or synchronous code blocks .
边栏推荐
- ‘make_ unique’ is not a member of ‘std’
- Learning notes on February 5, 2022 (C language)
- Bessie's weight problem [01 backpack]
- JMeter reading and writing excel requires jxl jar
- Go language modifies / removes multiple line breaks in strings
- Std:: vector minutes
- Completabilefuture of asynchronous tools for concurrent programming
- (translation) json-rpc 2.0 specification (Chinese version)
- [paper notes] rethinking and improving relative position encoding for vision transformer
- Basic syntax and common commands of R language
猜你喜欢

‘make_ unique’ is not a member of ‘std’

Afterword of Parl intensive learning 7-day punch in camp

Qcodeeditor - QT based code editor

Shared memory synchronous encapsulation

google_ Breakpad crash detection

MySQL field truncation principle and source code analysis

Js- get the mouse coordinates and follow them

Source code analysis of zeromq lockless queue

System Verilog — interface

Completabilefuture of asynchronous tools for concurrent programming
随机推荐
(1) Introduction
One question per day, a classic simulation question
QT database connection deletion
Design and implementation of timer
google_ Breakpad crash detection
If a thread overflows heap memory or stack memory, will other threads continue to work
[C language] 32 keyword memory skills
Qlogsystem log system configuration use
Go build reports an error missing go sum entry for module providing package ... to add:
Paddlepaddle paper reproduction course biggan learning experience
客户经理给的开户链接办理股票开户安全吗?我想开个户
[paper notes] rethinking and improving relative position encoding for vision transformer
Basic syntax and common commands of R language
One question per day, punch in
One code per day - day one
Kali SSH Remote Login
QT inline dialog
BM setup process
Arthas source code learning-1
QT loading third-party library basic operation