In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the example analysis of Thread in java. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Life cycle
Related methods
Interrupt (): the instance method sets the thread interrupt ID to true. If the thread is in wait/sleep, it will report java.lang.InterruptedException.
Interrupted (): static method, Thread.interrupted () resets the current thread interrupt identity
IsInterrupted (): the instance method gets the current thread interrupt identity
Join (): interrupts the current thread and waits for the calling thread to finish.
Thread.sleep (millis): current thread hibernates for x milliseconds without releasing holding locks
LockSupport.park (): interrupts the current thread without releasing the holding lock
Object.wait (millis): the current thread waits for x seconds, if it is not passed, it waits indefinitely, releasing the holding lock
Thread variable
ThreadLocal: shared within the same thread and cannot be passed to child threads
Life cycle: when the first value is set, the threadLocals in the current thread is associated with Thread.currentThread (), and a ThreadLocalMap is initialized for storage.
The Entry that stores content in ThreadLocalMap uses WeakReference weak references to facilitate garbage collection.
In the case of non-thread pool, when the key is a weak reference and does not continue to be used in the code (after the strong reference is removed, only the weak reference is left), it will be reclaimed by gc. The get method will clean up the value corresponding to the non-existent key, ensuring memory recovery.
When used in a thread pool, there is a risk of memory overflow because threads are not recycled and the strong reference Thread is not cleaned up. This hidden danger can be avoided by cleaning up the remove method after the use is completed.
There is also a hidden danger that the life cycle is too long and exists in memory overflow when initializing with static.
InheritableThreadLocal: initialized only when the thread pool is created, so the thread pool scenario can cause assignment logic confusion
Rewrite childValue (), getMap (Thread), createMap (Thread t, T firstValue) to realize the content assignment of child threads.
ThreadLocal/InheritableThreadLocal usage process:
TransmittableThreadLocal: it can be passed to the child thread, and the child thread operates independently. The new thread will also get the new value, which needs to be used in conjunction with TtlRunnable or by modifying the java startup parameters.
The official complete sequence diagram of the whole process:
Sort it out according to the official timing chart:
This is the end of this article on "sample Analysis of Thread in java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.