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/01 Report--
This article mainly shows you the "sample analysis of Thread classes in Java", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and study the "sample analysis of Thread classes in Java".
First, several common attributes of Thread
The Thread class is a class that JVM uses to manage threads, in other words, each thread has a unique Thread object associated with it.
Create threads in Java
Displays the inheritance Thread and overrides the run method to specify the code executed by the thread
The anonymous inner class inherits the Thread and overrides the run method to specify the code that the thread executes
Show the implementation of the Runnable interface and override the run method
Anonymous inner class to inherit the Runnable interface and override the run method
Describe the executed code through lambda expressions
Property gets the method IDgetId () name getNmame () state getState () priority getPriority () whether the background thread isDaemon () survives, whether isAlive () is interrupted isInterrupted ()
ID is the unique identity of a thread, and different threads will not repeat it.
The name is a situation in which various debugging tools use state to represent the current situation of the thread, which we will explain further below.
High-priority threads are theoretically easier to schedule to
With regard to background threads, it is important to keep in mind that JVM will not stop running until all non-background threads of a process are finished.
Whether or not to survive, that is, a simple understanding of whether the run method has finished running
The problem of thread interruption is explained further below.
Public static void main (String [] args) throws InterruptedException {Thread t = new Thread ("123") {@ Override public void run () {for (int I = 0; I < 10; iTunes +) {System.out.println (Thread.currentThread (). GetName ()); try {Thread.sleep (100) } catch (InterruptedException e) {e.printStackTrace ();}} System.out.println ("thread exit");}} / / this set of attributes, after the thread is created, the properties remain unchanged: System.out.println (t.getName ()); System.out.println (t.getPriority ()); System.out.println (t.isDaemon ()); System.out.println (t.getId ()). / / this set of properties will start to change System.out.println (t.isAlive ()); System.out.println (t.isInterrupted ()); System.out.println (t.getState ()); t.start (); while (t.isAlive ()) {System.out.println ("123 running") as the thread runs. System.out.println (t.getState ()); System.out.println (t.isInterrupted ()); Thread.sleep (300);}}
Second, thread debugging 1, start a thread
We've seen how to create a thread object by overriding the run method, but just because the thread object is created doesn't mean the thread starts running.
Overriding the run method is an instruction list that provides the thread with what to do
The thread object can be thought of as calling Li Si and Wang Wu over.
When you call the start () method, you shout, "Let's do it!" The thread really executes independently.
Static class MyThread extends Thread {@ Override public void run () {System.out.println ("I am a thread");}} public static void main (String [] args) {Thread t = new MyThread (); t.start ();} 2, interrupt a thread
Interrupts let a program end, and there may be two situations in which it ends.
1. The task has been completed.
2, halfway through the execution of the task, it is forced to end
Public static void main (String [] args) throws InterruptedException {Thread t = new Thread () {@ Override public void run () {while (! IsQuit) {System.out.println ("transferring money"); try {Thread.sleep (500);} catch (InterruptedException e) {e.printStackTrace ();}} System.out.println ("transfer terminated") }}; t.start (); Thread.sleep; System.out.println ("have a mole, terminate the transaction"); isQuit = true;}
Public static void main (String [] args) throws InterruptedException {Thread t = new Thread () {@ Override public void run () {while (! Thread.interrupted ()) {System.out.println ("transferring money"); try {Thread.sleep (5000) } catch (InterruptedException e) {e.printStackTrace (); break;}} System.out.println ("transfer termination");}}; t.start (); Thread.sleep (5000) System.out.println ("have a mole, terminate the deal"); t.interrupt ();}
Thread receives notifications in two ways:
1. If the thread is blocked and suspended by calling a method such as wait/join/sleep, it is known in the form of an InterruptedException exception and the interrupt flag is cleared
When InterruptedException occurs, whether or not to end a thread depends on how the code is written in catch. You can choose to ignore this exception, or you can jump out of the loop to end the thread.
two。 Otherwise, only an internal interrupt flag is set, and thread can pass through the
Thread.interrupted () determines that the interrupt flag of the current thread is set and clears the interrupt flag.
Thread.currentThread (). IsInterrupted () determines that the interrupt flag of the specified thread is set and does not clear the interrupt flag
Notification received in this way is more timely and can be received immediately even if the thread is in sleep.
Public static void main (String [] args) {Thread t = new Thread () {@ Override public void run () {for (int I = 0; I < 10; iTunes +) {System.out.println (Thread.interrupted ()); t.start (); t.interrupt () }
Public static void main (String [] args) {Thread t = new Thread () {@ Override public void run () {for (int I = 0; I < 10; iTunes +) {System.out.println (Thread.currentThread (). IsInterrupted ());}; t.start () T.interrupt ();}
3, wait for a thread
T1 and T2 serial execution
Public static void main (String [] args) throws InterruptedException {Thread T1 = new Thread () {@ Override public void run () {for (int I = 0; I < 10; iThread +) {System.out.println ("I am Thread 1"); try {Thread.sleep (50) } catch (InterruptedException e) {e.printStackTrace ();}; Thread T2 = new Thread () {@ Override public void run () {for (int I = 0; I < 10) IThread +) {System.out.println ("I am Thread 2"); try {Thread.sleep (50);} catch (InterruptedException e) {e.printStackTrace ();} T1.start (); t1.join (); t2.start (); t2.join (); System.out.println ("main thread finished");}
T1 and T2 concurrent execution
Public static void main (String [] args) throws InterruptedException {Thread T1 = new Thread () {@ Override public void run () {for (int I = 0; I < 10; iThread +) {System.out.println ("I am Thread 1"); try {Thread.sleep (50) } catch (InterruptedException e) {e.printStackTrace ();}; Thread T2 = new Thread () {@ Override public void run () {for (int I = 0; I < 10) IThread +) {System.out.println ("I am Thread 2"); try {Thread.sleep (50);} catch (InterruptedException e) {e.printStackTrace ();} T1.start (); t2.start (); t1.join (); t2.join (); System.out.println ("main thread finished");}
4. Sleep thread public static void main (String [] args) throws InterruptedException {System.out.println (System.currentTimeMillis ()); Thread.sleep (1000); System.out.println (System.currentTimeMillis ());}
1. If the thread is normally running the computational judgment logic and is queued in the ready queue, the scheduler will filter out the appropriate PCB from the ready queue and let it run on the CPU
2. If a thread calls sleep, it will put the PCB of the corresponding thread into the blocking queue, and the blocking queue cannot run on PCB.
3. When the time is up, automatically take the PCB back to the original ready queue
The above is all the content of the article "sample Analysis of Thread classes in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.