Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Java multithreading

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

How to understand Java multithreading, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

1. Look at the picture with questions

1) Lock of competing objects and competition for CPU resources as well as competition are awakened

2) when the lock is acquired and when the lock will be released

2. The same picture

3. Detailed illustration

1) Thread t = new Thread (), initialize a thread, which is actually a normal object, and its state is New

2) t.start (); thread is ready (runnable state), that is, waiting to run at any time, do not underestimate this start, this start determines whether it is a real thread instance, because start prepares a thread environment for it, if you just call the run method normally, then this is a common method. Threads at this time will compete for CPU resources, so whoever is competing for CPU resources, that is, dispatched Scheduler, can go from runnable state to real runnable state.

3) when the thread gets the CPU resource, the thread goes from the runnable state to the actual runnable state, that is, Running, and there is no doubt that it is running now.

4) if the thread is waiting for customer input to learn, that is, various blocking events such as IO exceptions, or it may have called blocking events such as sleep, the thread will change from running state to blocking state, and nothing will happen in this state!

5) once the blocking event is cleared, such as the user has completed input, the IO stream has been closed, and the sleep has timed out, the thread has changed from the blocking state to the ready state, returned to the runnable state again, and competed with other threads for resources at any time, waiting to run!

6) A running thread may encounter a synchronization method or synchronization block, that is, a method or block marked by synchronized, when the thread acquires a lock on the object and other threads cannot enter the synchronization method. What about these unexecutable threads? They are all blocked here, waiting for the lock to be released and competing for lock resources again, because only the thread that owns the lock is qualified to continue to run, so these threads are blocked in the lock pool (Lock Pool).

7) once the thread blocked in the lock pool competes for the lock (the previous thread runs out of exception internally, or calls wait, etc., the thread's lock will be released), then the thread will change from the blocking state to the ready state, do not think that the thread will execute immediately, this is impossible, you have to think that thread execution is to obtain CPU resources If there is no operating system scheduling, they are not qualified to run!

8) the running thread may enter the synchronization method or synchronization block while running, at this time he has the lock of the object, which is supreme, but because the current environment may make it unnecessary for him to continue to hold the line, he will give up the lock resources to give other threads a chance to continue to execute, so the thread may call the wait method of the object within the synchronized. The current thread gives up the lock resource and enters the waiting pool (wait pool) by itself until it is awakened by another thread! If they are not awakened, they will always be in the waiting pool, blocked by threads, so all they want at this time is to be awakened, because only waking up can continue to run!

9) once a thread blocked in the waiting pool is awakened (it may be a synchronized thread calling notify or notifyAll, or an external call to interrupt may cause an internal exception to be thrown and will also acquire a lock), then the thread will change from the waiting pool to the lock pool and continue to block, so don't think that the thread will continue to run when it is awakened, it is impossible, they also need to compete for lock resources.

10) if an exception is thrown while the thread is running, or the thread is actually finished, then the thread ends, that is, the extinction period. You cannot continue start after you have finished running. You must start a new new to start. Then there will be a life cycle.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report