Multithreading advanced =>JUC concurrent programming
Self foundation:
-
Java
-
The foundation of multithreading
1. What is JUC
Source + official documents interview frequently asked
Java.util toolkit package, classification
Business: Common Thread code Thread
Runnable returns no value and is less efficient than Callable!
2. Threads and processes
Threads, processes, if you can’t use a one-sentence spoken technology, not solid!
Process: a program, qi.exe music.exe program collection; .jar
A process can often contain multiple threads, at least one!
How many threads does Java have by default? The main, the GC
Thread: opened a process Typora, write, autosave (thread responsible)
For Java: Thread, Runnable, Callable
public synchronized void start(a) {
/** * This method is not invoked for the main method thread or "system" * group threads created/set up by the VM. Any new functionality added * to this method in the future may have to also be added to the VM. * * A zero status value corresponds to state "NEW". */
if(threadStatus ! =0)
throw new IllegalThreadStateException();
/* Notify the group that this thread is about to be started * so that it can be added to the group's list of threads * and the group's unstarted count can be decremented. */
group.add(this);
boolean started = false;
try {
start0();
started = true;
} finally {
try {
if(! started) { group.threadStartFailed(this); }}catch (Throwable ignore) {
/* do nothing. If start0 threw a Throwable then it will be passed up the call stack */}}}// the local method, the underlying C++, Java can not directly manipulate the hardware
private native void start0(a);
Copy the code
Can Java really start threads? Can’t open
Concurrent, parallel
Concurrent programming: concurrent, parallel
Concurrency (multiple threads working on the same resource)
- CPU a core, simulation out of a number of threads, the world martial arts, only fast not broken, fast alternating
Parallel (more than one person walking together)
-
CPU multi-core, multiple threads can execute at the same time; Highest performance –> Thread pool
package com.chao.demo01; public class Test1 { public static void main(String[] args) { // Get the number of CPU cores //CPU intensive, IO intensiveSystem.out.println(Runtime.getRuntime().availableProcessors()); }}Copy the code
The essence of concurrent programming: Make full use of CPU resources
All companies value it!
Business, make money => increase efficiency, lay off people, find a good person to replace three bad people;
Personnel (minus), technical cost (high)
Threads have several states
public enum State {
/ / new
NEW,
/ / run
RUNNABLE,
/ / blocking
BLOCKED,
// Wait, wait, wait
WAITING,
// Wait for timeout
TIMED_WAITING,
/ / termination
TERMINATED;
}
Copy the code
Wait/sleep
1. From different classes
wait => Object
sleep => Thread
In the enterprise, sleep
2. Release of locks
Wait releases lock, sleep does not release lock.
3. The scope of use is different
Wait must be in the synchronized code block
Sleep can be anywhere
4. Whether exceptions need to be caught
Wait does not need to catch exceptions
Sleep must catch exceptions