当前位置:网站首页>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 .
边栏推荐
- How to download and install Weka package
- QT database connection deletion
- Kali modify IP address
- 2.18 codeforces supplement
- Postman usage notes, interface framework notes
- ‘make_ unique’ is not a member of ‘std’
- Two advanced playing methods of QT signal and slot
- QT article outline
- [paper notes] mcunetv2: memory efficient patch based influence for tiny deep learning
- BM setup process
猜你喜欢

Learning to Measure Changes: Fully Convolutional Siamese Metric Networks for Scene Change Detection

GDB debugging

One question per day,

MySQL field truncation principle and source code analysis
![[paper notes] poly yolo: higher speed, more precise detection and instance segmentation for yolov3](/img/28/6d58759a4a4b18923a5ed5ed573956.jpg)
[paper notes] poly yolo: higher speed, more precise detection and instance segmentation for yolov3

Graphic control and layout basis of R visualization

Character encoding minutes

JSON module dictionary and string conversion
![[paper notes] street view change detection with deconvolutional networks](/img/2d/777fd0d85ff4d349516b95923410fd.jpg)
[paper notes] street view change detection with deconvolutional networks

Distributed token
随机推荐
User defined data type - structure
Single user mode
Go build reports an error missing go sum entry for module providing package ... to add:
Install Kali extension 1: (kali resolution problem)
Installing QT plug-in in Visual Studio
Judging the number of leap years from 1 to N years
QT source code online view
Generation method and usage of coredump
Shared memory synchronous encapsulation
QT animation loading and closing window
google_ Breakpad crash detection
[paper notes] semi supervised object detection (ssod)
Iterator failure condition
[untitled] PTA check password
QT excel table read / write library - qtxlsx
QT set process startup and self startup
CPU over high diagnosis and troubleshooting
[C language] 32 keyword memory skills
(1) Introduction
Dynamic memory allocation