Hello, today for everyone to share the Java thread, quickly take out a small notebook to write it down
Understanding threads
concept
- Threads belong to the composition of processes (each process has at least one thread (main thread), but can have multiple threads).
- Threads are the smallest unit of OS scheduling.
- Processes are the smallest unit of system allocation of resources (including CPU), and threads are the smallest unit of system scheduling (CPU allocation)
When are threads needed
When the original execution flow (scheduling unit) is blocked by some event and we need to run some other code
We need some scheduling units to speed up our code
Creating a Java thread
Several common properties of Thread
How do I create a Java thread
- Thread class – Each Thread object represents a Thread
- Runnable Interface – Each Runnable object is a task
The Thread class is used by the JVM to manage threads, each of which has a unique Thread object associated with it.
Example:
- Threads inherit from the Thread class and override the Run method.
- Implement the Runnable interface, override its Run method, instantiate a Runnable object, and pass the Runnable object in when constructing the Thread object.
Thread objects can also be used as Runnable objects. The phenomenon of using multiple threads has randomness, and the source of randomness is scheduling mechanism
start() vs run()
Overriding the run method provides what the thread needs to do, while calling start is truly executed independently. To start a thread, call the start method
Common constructor of Thread class:
Thread the interrupt
Most of our threaded code runs in an infinite loop
1. Stop it with some shared data
The Thread class provides methods to stop it
You can think of each Thread object as having a similar variable to stop it.
A method is called
Other threads can call.interrupt ();
Within the Thread that needs to be stopped, call Boolean thread.interrupted ();
There may be two states within it:
1. If the sleep method is blocked, InterruptedException is received indicating that it needs to stop.
2. In the ready state, executing code,
Thread.interrupted() returns true, meaning that it is required to stop
It can only be recommended to stop, not forced to stop
Thread.interrupted() clears the break flag after reading it once
Use.isinterrupted () to set the interrupt flag for a specified thread without clearing the interrupt flag.
Waiting for a thread
Sometimes we need to wait for a thread to finish its work before we can proceed, for example: thread A waits for thread B to finish.
Using the join ();
Such as:
Thread state
The theoretical states of a thread are: initial, ready, running, and stopped
The Java language is TERMINATED as NEW, RUNNABLE, TERMINATED, BLOCKED, WAITING, and TIMED_WAITING
Thread.isXXX()
Alive: Which states are alive?
TERMINATED, BLOCKED, WAITING, and TIMED_WAITING
(Ready, running, blocking)
Daemon: background threads vs default
Background threads do things that do not affect the main threads, but need to do in silence: 1) garbage collection mechanism, 2) downloading thread behind the listening song
Settings:
Background thread: thread.setdaemon (true);
Foreground thread: thread.setdaemon (false);
When a JVM process terminates:
All foreground threads finish running (1, background threads do not affect, 2, regardless of whether the main thread)
Common static methods in Thread
1, thread. sleep(ms); = = TimeUtil. MILISECONDS. Sleep (ms);
Thread.currentthread ();
Returns a reference to the object of the thread calling the method
3, Thread. The yield (); Voluntarily abandon CPU;
Such as:
Observe thread state tools
The jConsole tool is officially installed in the JDK
1. Start program (program with thread running)
2. Open the jConsole tool (in the JDK installation directory, jconsole.exe under the bin directory).
3. Select the program to observe
Well, this is the end of today’s article, I hope to help you confused in front of the screen