In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Good programmers share the thread advanced part of big data's tutorial. Let's first talk about the life cycle of threads.
For a thread, after it is created, it does not immediately enter the running state, nor is it always running. In the thread's declaration cycle, a thread will switch between multiple states.
New: newborn state, thread is instantiated, but execution has not started (start)
Runnable: ready state, start has been executed, thread has been started, but did not grab the CPU time slice
Running: running status, grabbing CPU time slices
Blocked: blocking state, thread execution process, encountered some special circumstances, will enter the blocking state. Threads in blocking cannot be preempted by parameter time slices (cannot be scheduled by thread scheduler)
Dead: dead state, 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, it "locks" the resource before accessing it. At this time, 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 a blocking state and waits for unlocking.
Synchronization code snippet / / synchronization code snippet / / parentheses: lock / / curly braces: synchronization code snippet, in general Synchronized () {} / about synchronization locks: can be divided into two types: object locks, class locks / / synchronization methods / / methods modified with the synchronized keyword are synchronization methods / / synchronize all code in a method / / equivalent to putting all code in a method into a synchronized code segment / / synchronization method locks: / / 1. If this method is a non-static 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
It's just a RenntrantLock class.
Thread deadlock (understand)
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, where 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 pattern
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:
Both methods can cause a thread to enter blocking.
Difference: the wait method releases the lock tag, while sleep does not release the lock tag. Thread Safety problems 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 maximum number of threads in the thread pool.
Long keepAliveTime: the amount of time that a temporary thread other than the core thread can survive. (from when this thread is idle)
TimeUnit unit: the unit of time above
NANOSECONDS: nanosecond
MICROSECONDS: microsecond
MILLISEONDS: milliseconds
SECONDS: seconds
MINUTES: points
HOURS: when
DAYS: God
BlockingQueue workQueue: waiting queue
ArrayBlockingQueue
LinkedBlockingQueue
SynchronousQueue
RejectedExecutionHandler handler: access denied policy
Preview directions:
Network programming
TCP
UDP
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.