In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
A comprehensive overview of Java threads, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
What is a thread?
A thread is a part of a process and an execution route in program execution.
A process refers to an execution activity of a program in its own address space, which is the basic unit for a program to run independently.
A process can contain multiple threads, and a thread corresponds to an execution route in a process.
How many ways can threads be created?
There are four main ways to create threads:
Method 1: inherits the Thread class, overrides run (), and no return value
Method 2: implement the Runnable interface, rewrite run (), and no return value
Method 3: create a new FutureTask + to implement the Callable interface, rewrite call (), and return a value
Method 4: create thread pool through Executors tool class + call submit + rewrite Callable interface, rewrite call (), and return value
The specific description and code implementation of each creation method are as follows:
/ * four ways to create Thread threads: inner class writing * / public class NewThread {public static void main (String [] args) throws Exception {/ / Mode 1 Thread T1 = new Thread () {@ Override public void run () {System.out.println ("Mode 1: inherits the Thread class and overrides the run () method to create a thread with no return value") }; t1.start (); Thread.sleep (1000); / / Mode 2 Thread T2 = new Thread (new Runnable () {@ Override public void run () {System.out.println ("Mode 2: implement the Runnable interface and override the run () method to create a thread with no return value") }}); t2.start (); Thread.sleep (1000) / / Mode 3 FutureTask ft = new FutureTask (new Callable () {@ Override public String call () throws Exception {String result = "Mode 3: implement the Callable interface and override the call () method to create a new FutureTask object as a new Thread instantiation parameter to create a thread, with a return value"; return result;}}) Thread T3 = new Thread (ft); t3.start (); System.out.println (ft.get ()); / / output return value Thread.sleep (1000); / / Mode 4 ExecutorService pool = Executors.newFixedThreadPool (5) Future future = pool.submit (new Callable () {@ Override public String call () throws Exception {String result = "method 4: create a thread pool through the utility class Executors, call submit to create a new Future object and rewrite the Callable API to rewrite the call () method to create a thread with a return value"; return result;}}); pool.shutdown () / / close the thread pool System.out.println (future.get ()); / / output the return value}} what are the states of the thread?
New (new): create a new thread in one of the ways described above
Ready (Ready): call the thread's start () method, first enter the ready state and wait for the CPU time to be obtained
Running: threads in the ready state can enter the running state when they get the CPU time or the thread resumes the blocking state.
Blocked: threads in running state may enter blocking state either because of IO blocking or in synchronized synchronization code blocks
Death (Dead): a thread that is normally running in the end or ready state will enter the dead state by directly calling the stop () method.
Sleeping: calling the sleep method to specify how long the thread sleeps will release CPU resources, but will not release lock resources. When sleep time arrives, it will return to the ready state.
Waiting: calling wait leaves the thread waiting for a short time, frees CPU resources, and releases lock resources into a ready state.
What are the core methods and functions related to threads?
Start: calling the underlying source code of the start () method will determine whether the thread state is a new state, if not, throw an exception directly, and then call a native local method start0, whose underlying layer will schedule through JVM and finally call the run () method to execute.
Run: call the run () method, and the underlying layer goes directly to the rewritten run () method and executes the contents of the code block
Sleep: a native native static static method belonging to the Thread class, which can call the sleep (1000) method anywhere, during which the current thread is put to sleep for 1 second and the CPU resource is released without releasing the lock resource.
Wait: a method belonging to the Object class, which can only call the wait (1000) method in the synchronized synchronization block, during which the current thread enters the waiting state for 1 second, which not only frees up the CPU time, but also releases and releases the object lock resources.
Yield: like sleep, it is also a native native static static method of the Thread class. The biggest difference from sleep is that Thread.yield () does not need to specify a pause time and does not block the thread, but enters the ready state and briefly gives up the CPU resource. The CPU resource may be obtained again by itself, depending on the scheduler.
Notify: like wait, it is a method of the Object class that wakes up the waiting thread after wait () individually and enters the Read ready state.
NotifyAll: like notify, it is also a method of the Object class that wakes up all waiting threads on the current object and enters the Read ready state
Stop: this method has been abandoned and is not recommended. The function of this method is to end the thread directly and enter the dead state.
Interrupt: to break a thread, there is no guarantee that the thread will be dead, ready or running. You don't want stop to break a running thread directly. What's up! Don't give me a like. This finger is made of gold.
After reading the above, have you mastered the method of a comprehensive overview of Java threads? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.