当前位置:网站首页>Thread safety and processing caused by multithreading
Thread safety and processing caused by multithreading
2022-06-30 04:39:00 【Beginner 891】
1. Multithreading
(1) What is multithreading ?
Multithreading means that multiple threads can execute tasks in an application . When multiple tasks need to be executed at the same time in the program , You need multithreading .
(2) The advantages and disadvantages of multithreading
advantage : Speed up the program ;
Improved cpu Utilization ratio ;
Improve program structure .
shortcoming : Multi wire programs consume more memory ;
Multithread switch execution , Yes cpu The requirements of ( These two disadvantages can be solved by upgrading the hardware equipment );
Multiple threads access the same shared resource , There will be thread safety issues appear Thread safety problem .
Single multithreading does not cause thread safety problems , Each thread is doing its own thing .
(3) Thread parallelism and concurrency
Thread parallel execution : Many people do different things at the same time , Mutual interference .
Threads execute concurrently : To perform something in a period of time .
2. Solving thread safety problems
To solve the thread safety problem, we must lock the program ,
(1)synchronized
synchronized keyword
synchronized Code blocks can be decorated
synchronized( Synchronization lock object ){ }
synchronized When modifying a method :
If it's a non static method , So the lock object is this ; If it's a static method , So the lock object is a class Class object ( There is only one class Class object ).
synchronized It is realized by low-level instruction control .
synchronized Code blocks can be decorated , Modification methods , The object of the lock ( The lock object may change ).
synchronized Locking is implicit , When entering the synchronized code block , Automatically acquire locks , After the synchronization code block is executed , Automatic release lock .
(2) Lock Interface
Lock The lock depends on java Code to control ,ReentrantLock Class implements the Lock Interface , Can control and synchronized Same function .. But the implementation details of the two are completely different ,Lock Locking can only lock a certain piece of code , And it is the lock and release lock shown .
3. Deadlock
Reasons for appearance : Incorrect locking may lead to deadlock . That is, different threads occupy the synchronization resources required by each other without giving up .
consequence : After deadlock , The program will not report an error , Just waiting ..
Solution : Deadlock is easy to occur when locks are nested , When locking , Consider the sequence of locks , Minimize nested use .
4. Thread communication
(1) Thread communication
Thread communication means that multiple threads are processing the same resource , And the tasks are different , Thread communication is needed to help solve the use or operation of the same variable between threads . When multiple threads operate on the same data , Avoid contention for the same shared variable . Multiple threads hold each other back , Restrict operation .
(2) Method
wait() Let thread wait , call wait() Method, the thread enters a blocking state , Must wake up through another thread
notify() Wake up waiting thread
notifyAll() Wake up all threads waiting
this 3 Methods must be executed in synchronized code blocks .
sleep() and wait()
The same thing : Can make the thread enter the blocking state .
Difference : sleep() yes Thread Class , It won't release the lock , When the sleep time is up , Will automatically enter the ready state ;
wait() yes Object Class ,wait() It can release the lock ,wait() Thread after , Need to use notify() perhaps notifyAll() Wake up the
边栏推荐
- Serializable and Deserialize
- Five methods to clear floating and their advantages and disadvantages
- Blocking queue example
- IO stream, byte stream read / write copy
- QT creator 8 beta2 release
- Threejs实现模拟河流,水面水流,水管水流,海面
- What is SQL injection and how to avoid it?
- 【Paper】2021_ Uniformity of heterogeneous hybrid multi-level intelligent systems using UGV and UAV
- Anonymous pipeline for interprocess communication
- What is an optocoupler circuit and what should be paid attention to in actual use?
猜你喜欢

SSL update method

File system and directory operations

【Paper】2019_ Distributed Cooperative Control of a High-speed Train

Blocking queue example

Network high concurrency

Bean创建流程 与 lazy-init 延迟加载机制原理

Explain the underlying principles of JVM garbage collection in simple terms

Connect to the database and run node JS running database shows that the database is missing

Configure specific source IP in SLA detection of FortiGate sdwan

【Paper】2017_ Research on coordinated control method of underwater vehicle formation marine survey
随机推荐
Software digital signature certificate
Intern method of string
Code cloud fatal: authentication failed for
[UGV] schematic diagram of UGV version 32
The most comprehensive summary notes of redis foundation + advanced project in history
Es2019 key summary
Process architecture and process management
How to repair expired SSL certificates?
Es2017 key summary
Redis实现短信登入功能(一)传统的Session登入
Myrpc version 5
One interview question a day - the underlying implementation of synchronize and the lock upgrade process
One interview question every day to talk about the process of TCP connection and disconnection
Bean创建流程 与 lazy-init 延迟加载机制原理
Equity interest [non DP]
Troubleshooting of abnormal communication between FortiGate and fortiguard cloud
FortiGate firewall quick initialization administrator password
Redis implements SMS login function (I) traditional session login
[control] multi agent system summary. 5. system consolidation.
What to do when the alicloud SSL certificate expires