In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what's the difference between thread, multithread and thread pool". The explanation in this article is simple and clear and easy to learn and understand. let's go deep into the editor's train of thought. Let's study and learn "what are the differences between threads, multithreads and thread pools"?
one。 Thread
In the operating system, a thread is a smaller basic unit that can run independently than a process. At the same time, it is the basic unit of CPU scheduling. The thread itself basically does not own system resources, but only has some system resources that need to be used at run time, such as program counters, registers, stacks and so on. All threads in a process can share all resources in the process.
two。 Multithreading
Multithreading can be understood as being able to run multiple different threads to perform different tasks in the same program, and these threads can run using multiple cores of CPU at the same time. Multithreaded programming can maximize the use of CPU resources. If the processing of a thread does not need to take up CPU resources (for example, IO threads), you can make the current thread give up CPU resources to allow other threads to obtain CPU resources, and then be able to perform tasks corresponding to other threads, so as to maximize the use of CPU resources.
three。 The implementation of threads
In Java, there are three ways to implement threads. By inheriting the Thread class and implementing the Runnable interface, the Callable interface is realized. The simple sample code is shown below.
1) inherit the Thread class code package io.binghe.concurrent.executor.test;/** * @ author binghe * @ version 1.0.0 * @ description inherits the Thread implementation thread * / public class ThreadTest extends Thread {@ Override public void run () {/ / TODO writes the business logic executed in the thread}} 2) the code package io.binghe.concurrent.executor.test that implements the Runnable interface / * * @ author binghe * @ version 1.0.0 * @ description implements the Runnable implementation thread * / public class RunnableTest implements Runnable {@ Override public void run () {/ / TODO writes the business logic executed in the thread}} 3) the code package io.binghe.concurrent.executor.test;import java.util.concurrent.Callable that implements the Callable interface / * * @ author binghe * @ version 1.0.0 * @ description implements the Callable implementation thread * / public class CallableTest implements Callable {@ Override public String call () throws Exception {/ / TODO here writes the business logic return null;}} executed in the thread. Thread lifecycle 1) lifecycle
A thread needs to go through a variety of different states from creation to final demise, and these different thread states constitute different stages of the thread life cycle from beginning to end. The life cycle of a thread can be summarized as follows.
Among them, several important states are shown below.
NEW: initial state, the thread is built, but the start () method has not been called yet.
RUNNABLE: runnable state, which can include: running state and ready state.
BLOCKED: blocking state in which a thread needs to wait for another thread to release the lock or wait to enter the synchronized.
WAITING: indicates a waiting state in which a thread needs to wait for other threads to notify or interrupt it before moving on to the next state.
TIME_WAITING: timeout wait status. You can return on your own at a certain time.
TERMINATED: terminated status, the current thread has finished execution.
2) Code example
In order to better understand the life cycle of a thread and the individual states in the life cycle, the code example is then used to output each state information of the thread.
WaitingTime
Create the WaitingTime class and call the TimeUnit.SECONDS.sleep (long) method in the while (true) loop to verify the thread's TIMED_WARTING state, as shown below.
Package io.binghe.concurrent.executor.state;import java.util.concurrent.TimeUnit;/** * @ author binghe * @ version 1.0.0 * @ description thread hibernates continuously * / public class WaitingTime implements Runnable {@ Override public void run () {while (true) {waitSecond (200) How many seconds does the thread wait public static final void waitSecond (long seconds) {try {TimeUnit.SECONDS.sleep (seconds);} catch (InterruptedException e) {e.printStackTrace ();} WaitingState
Create the WaitingState class, which acquires the synchronized lock of the current class Class object in a while (true) loop, that is, no matter how many instances the class creates, the synchronized lock is the same and the thread is in a waiting state. Next, use the wait () method of the Class object of the current class in synchronized to verify the WAITING state of the thread, as shown below.
The package io.binghe.concurrent.executor.state;/** * @ author binghe * @ version 1.0.0 * @ description thread waits on Warting for * / public class WaitingState implements Runnable {@ Override public void run () {while (true) {synchronized (WaitingState.class) {try {WaitingState.class.wait () } catch (InterruptedException e) {e.printStackTrace ();} BlockedThread
BlockedThread mainly calls the TimeUnit.SECONDS.sleep (long) method in the while (true) loop in the synchronized code block to verify the BLOCKED state of the thread. When two BlockedThread threads are started, the first thread is in the TIMED_WAITING state, and the later thread is in the BLOCKED state. The code is as follows.
Package io.binghe.concurrent.executor.state;/** * @ author binghe * @ version 1.0.0 * @ description will not release the lock after locking * / public class BlockedThread implements Runnable {@ Override public void run () {synchronized (BlockedThread.class) {while (true) {WaitingTime.waitSecond (100);} ThreadState
Start each thread and verify the status of each thread's output, as shown below.
Various states of package io.binghe.concurrent.executor.state;/** * @ author binghe * @ version 1.0.0 * @ description thread, test thread life cycle * / public class ThreadState {public static void main (String [] args) {new Thread (new WaitingTime (), "WaitingTimeThread"). Start (); new Thread (new WaitingState (), "WaitingStateThread"). Start () / / the BlockedThread-01 thread grabs the lock, and the BlockedThread-02 thread blocks new Thread (new BlockedThread (), "BlockedThread-01") .start (); new Thread (new BlockedThread (), "BlockedThread-02") .start ();}}
Run the ThreadState class, as shown below.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.