In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "Java multithreaded state and method instance parsing". In daily operation, I believe many people have doubts about Java multithreaded state and method instance parsing. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "Java multithreaded state and method instance parsing". Next, please follow the editor to study!
First, we introduce the five states of a thread:
New Ecology: New Thread ()
Ready: get ready to grab the CPU time slice
Running mode: grabbed the CPU time slice
Blocking state: give up the CPU time slice that has been grabbed, and do not participate in the competition for the time being
Dead state: after Run has finished running
Next, we introduce three methods: thread blocking, thread priority setting, and thread comity.
Public class MutliThreadDemo4 {public static void main (String [] args) {threadBlock (); / / threadPriority (); / / threadYield ();} / * * Thread blocking * / private static void threadBlock () {/ / create the object Runnable r = ()-> {for (int I = 0; I) of the Runnable interface implementation class
< 10; i++) { System.out.println(Thread.currentThread().getName() + ":" + i); //线程休眠(由运行状态到阻塞状态,时间过了回到就绪态,重新争抢),直观上表现为停顿打印 try { Thread.sleep(1000); }catch(InterruptedException e){ e.printStackTrace(); } } }; //实例化 new Thread(r, "甲").start(); } /** * 线程的优先级 */ private static void threadPriority() { Runnable r = () ->{for (int I = 0; I < 10; iTunes +) {System.out.println (Thread.currentThread (). GetName () + ":" + I);}; Thread T1 = new Thread (r, "A"); Thread T2 = new Thread (r, "B"); / / set the priority of the thread before starting execution (start), just modify the probability that the thread can grab the CPU time slice. / / it is not necessarily that the thread with high priority can grab the CPU time slice / / priority setting. It is an integer (0d10), and the default is 5 t1.setPriority (10); t2.setPriority (1); t1.start (); t2.start () } / * Thread comity * / private static void threadYield () {/ / Thread releases its own CPU resources and returns to ready state / / anonymous inner class Runnable r = new Runnable () {public void run () {for (int I = 0; I < 10; iThread +) {System.out.println (Thread.currentThread (). GetName () + ":" + I); if (I = 3) {Thread.yield () }; Thread thread1 = new Thread (r, "thread-1"); Thread thread2 = new Thread (r, "thread-2"); thread1.start (); thread2.start ();}}
At this point, the study on "Java multithreaded state and method instance parsing" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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: 252
*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.