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)05/31 Report--
Today, I would like to share with you the relevant knowledge points of thread state instance analysis of Java concurrent programming. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
Overview of thread state
The complete process of a thread from birth to death:
When a thread is created and started, it neither enters the execution state as soon as it starts, nor is it in the execution state all the time. How many states are there in the life cycle of a thread? Six thread states are given in the java.lang.Thread.State enumeration in API:
Thread state causes state occurrence condition NEW thread has just been created but has not been started. The start method has not been called yet. MyThread t = new MyThread has only thread objects and no thread characteristics. The state in which a Runnable (runnable) thread can run in a java virtual machine may or may not be running its own code, depending on the operating system processor. The t.start () method is called: ready (classical teaching) Blocked (lock blocking) when a thread tries to acquire an object lock and the object lock is held by another thread, the thread enters the Blocked state; when the thread holds the lock, the thread becomes Runnable state. Waiting (infinite waiting) A thread enters a Waiting state while waiting for another thread to perform a (wake-up) action. You cannot wake up automatically after entering this state. You must wait for another thread to call the notify or notifyAll method before you can wake up. Timed Waiting (timing and waiting) is the same as the waiting state, and several methods have timeout parameters, and calling them will enter the Timed Waiting state. This state will be maintained until the timeout expires or a wake-up notification is received. The common methods with timeout parameters are Thread.sleep and Object.wait. Teminated (terminated) dies because the run method exits normally, or because the run method is terminated by an exception that is not caught.
Sleep sleep method
There is a state in the state called timing wait, which can be demonstrated through the methods of the Thread class. Public static void sleep (long time) puts the current thread to sleep and wakes up automatically after millisecond to continue execution.
/ / the main thread executes until the sleep method hibernates for 1 second before continuing to execute public class Test {public static void main (String [] args) {for (int I = 1 begin wait I {try {System.out.println ("begin wait...."); synchronized (") {".wait ()) } System.out.println ("over");} catch (Exception e) {}}) .start ();}
Public void notify (): wakes up the thread in the waiting state on the current lock object this method must be called by the lock object.
Public class Demo2_notify {public static void main (String [] args) throws InterruptedException {/ / step 1: the child thread opens, enters an infinite waiting state, is not awakened, and cannot continue to run. New Thread (()-> {try {System.out.println ("begin wait...."); synchronized ("") {".wait ();} System.out.println (" over ") } catch (Exception e) {}}) .start (); / step 2: after adding the following code, 3 seconds later, the notify method will be executed to wake up the thread in wait. Thread.sleep (3000); new Thread (()-> {try {synchronized (") {System.out.println (" wake up ");" .wake ();}} catch (Exception e) {}}) .start () }} A small example of waiting to wake up
Define a collection, the steamed stuffed bun shop thread completes the production of the steamed stuffed bun, and the steamed stuffed bun is added to the collection; the foodie thread finishes buying the steamed stuffed bun, and the steamed stuffed bun is removed from the collection.
When there is no steamed stuffed bun (the steamed stuffed bun status is false), the foodie thread waits.
The steamed stuffed bun shop thread produces steamed buns (that is, the steamed stuffed bun status is true) and notifies the foodie thread (relieving foodie of the waiting state)
Public class BaoZiPu extends Thread {private List list; public BaoZiPu (String name,ArrayList list) {super (name); this.list = list;} @ Override public void run () {int I = 0 While (true) {/ / list enters the waiting state try {as the thread of the lock object synchronized (list) {if (list.size () > 0) {/ / storage element List.wait () } catch (InterruptedException e) {e.printStackTrace () }} / / if the thread does not enter the wait state, there is no element in the collection / / add the element list.add ("steamed buns" + iTunes +); System.out.println (list) to the collection / / there are already elements in the collection to wake up the thread getting the element list.notify ();} public class ChiHuo extends Thread {private List list; public ChiHuo (String name,ArrayList list) {super (name); this.list = list } @ Override public void run () {/ / write an endless loop while (true) {/ / because the same collection list is used as the lock object synchronized (list) {/ / if there are no elements in the collection The thread that gets the element enters the waiting state if (list.size () = = 0) {try {list.wait () } catch (InterruptedException e) {e.printStackTrace ();}} / / if there is an element in the collection, the thread that gets the element gets the element (delete) list.remove (0) / / print that there are no elements in the collection System.out.println (list); / / if there are no elements in the collection, wake up threads that add elements to the collection to add elements list.notify () } public class Demo {public static void main (String [] args) {/ / wait for wake-up case List list = new ArrayList (); / / create thread object BaoZiPu bzp = new BaoZiPu ("steamed stuffed bun shop", list); ChiHuo ch = new ChiHuo ("foodie", list) / / start thread bzp.start (); ch.start ();}} these are all the contents of the article "Thread State instance Analysis of Java concurrent programming". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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.