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 > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what are the advanced parts of big data thread?" in the operation of the actual case, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
First of all, let's talk about the life cycle of threads.
> for a thread, it does not enter the running state immediately after it is created, nor is it running all the time. In the thread's declaration cycle, a thread will switch between multiple states.
>
>
>
> new: new state, thread is instantiated, but execution has not started (start)
>
> runnable: ready state, start has been executed, thread has been started, but CPU time slice has not been grabbed
>
> running: running status, grabbing CPU time slices
>
> blocked: blocking state. When a thread encounters some special circumstances, it will enter blocking state. Threads in blocking cannot be preempted by parameter time slices (cannot be scheduled by thread scheduler)
>
> dead: dead status, thread termination
>
Normal death: end of code execution in the run method
>
Abnormal death: force to stop this thread using the stop method
# critical resource problem
Because resources are shared between threads. If there are multiple threads that operate on a data at the same time, there will be a problem with that data.
> if a thread is accessing a critical resource, "lock" the resource before accessing it. If other threads also need to access the critical resource, you need to check whether the resource is locked. If it is not locked, the thread can access the resource. If it is locked, the thread enters the blocking state and waits for unlocking.
# synchronize code snippets
> ````java
> / / synchronize code snippets
> / / parentheses: lock
> / / curly braces: synchronize code snippets. In general, write operations on critical resources.
> synchronized () {
>
>}
> / / about synchronization locks: they can be divided into two types: object locks and class locks
> / /
> ```
>
# synchronization method
> ````java
> / / the method modified with the synchronized keyword is the synchronization method.
> / / synchronize all the code in a method
> / / is equivalent to putting all the code in a method into a synchronized code snippet
> / / Lock of synchronization method:
> / / 1. If this method is a nonstatic method: the lock is this
> / / 2. If this method is a static method: the lock is a class lock (current class .class)
> private synchronized void sellTicket () {
>}
> ```
>
# lock and unlock
> is a class RenntrantLock
# Thread deadlock
In solving the critical resource problem, we introduce a concept of "lock". We can protect a resource with a lock. In fact, in a multithreaded environment, a situation may occur:
> suppose there are two threads An and B, in which thread A holds the lock tag an and thread B holds the lock tag B. at this time, thread A waits for the release of the lock tag b, and thread B waits for the release of the lock tag a. This situation is called deadlock.
# producer-consumer design mode
> wait (), notify (), notifyAll ()
>
> wait (): wait. Causes the current thread to release the lock mark and enter the waiting queue. You can put the current thread into a blocking state.
>
> notify (): wake up, wake up a thread in the waiting queue.
>
> notifyAll (): wake up, wake up all threads in the waiting queue.
> the difference between wait and sleep:
>
> 1. Both methods can cause a thread to enter blocking.
> 2. Difference: the wait method releases the lock tag, while sleep does not release the lock tag.
# Thread safety issues in lazy singleton design pattern
# Thread pool
> the ThreadPoolExecutor class is the core class of the thread pool. Several parameters in the constructor of this class:
>
> int corePoolSize: number of core threads. Core pool size.
>
> int maxmiunPoolSize: the largest number of threads in the thread pool.
>
> long keepAliveTime: how long a temporary thread other than the core thread can survive. (from when this thread is idle)
>
> TimeUnit unit: the time unit above
>
> NANOSECONDS: nanosecond
>
> MICROSECONDS: microseconds
>
> MILLISEONDS: milliseconds
>
> SECONDS: seconds
>
> MINUTES: points
>
> HOURS: hour
>
> DAYS: days
>
> BlockingQueue workQueue: waiting queue
>
> ArrayBlockingQueue
>
> LinkedBlockingQueue
>
> SynchronousQueue
>
> RejectedExecutionHandler handler: deny access policy
This is the end of the content of "what is the advanced part of big data thread"? thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.