Get the App
SLTechnology News&Howtos  ›  Development  › 

Instance parsing of Java multithread state and method

Shulou Source: shulou.com Published: 2022-06-03 14:02:06 09月22日 Update

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!

Tags: Thread state method priority time instance learning running blocking next integer more help practical just done object interface article probability Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Linux Shulou Technology Redmi Apple