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 > Servers >
Share
Shulou(Shulou.com)05/31 Report--
The main content of this article is to explain why the run () method should be turned on automatically. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "Why should the run () method be turned on automatically?"
Cpu is random, and the thread grabs the cpu before it can work, so the run () method must be started automatically by the strat () method, so cpu gets a signal that the thread can preempt cpu resources.
Run the garbage collector manually
Principle: when gc is executed, it triggers the garbage collection mechanism, starts the garbage collection thread, and executes the finalize method
The characteristic of cpu: the relationship between multiple threads is to grab cpu, and cpu is random.
The main function ends, the director area ends, the main thread ends with the end of the task, and the thread starts with the beginning of the task.
Create thread
By default, both the main thread and garbage collection thread are created by the system
① creates thread objects with Thread
Thread thread1=new Thread (); / / just create a Thread object
Thread1.start (); / / start the thread
The run () method in the Thread class is empty by default, so the start () method executes the result.
So you need to inherit the Thread class to override the run () method
If the run () method is called manually, the thread corresponding to run () corresponds to the thread in which the method is called.
The Thread class implements the Runnable interface, and there is only the run () method in the Runnable interface. The run () method implemented in the Thread class is only an empty square body and needs to be inherited to override it.
Create a class A that implements the Runnable interface and override the run () method
An a=new A ()
Thread to=new Thread (A)
To.start ()
Lock the thread conflict part of a method
Lock condition
① lock must be an object
② is to be shared by all threads
Synchronization code block
Synchronized (this) {Code Block}
Synchronization function
Non-static public synchronized void xxx () {} / / the default lock is this synchronized (this)
Static public synchronized static void xxx () {} / / the default lock is the bytecode file synchronized (xx.class) of the current class
Notify (terminating thread hibernation) wait (thread hibernation, do not preempt cpu)
Wait ()-- > must be used in a synchronous environment. Lock calls must be used to execute this line of code.
Notify ()-- > wakes up the thread under the same lock
Lock (show synchronization) replace Synchronized (implicit synchronization)
/ / create lock object Lock lock=new ReentrantLock ()
/ / Condition for production tasks
Condition proCon = lock.newCondition ()
/ / condition used to end the task
Condition conCon = lock.newCondition ()
/ / unlock
Lock.lock ()
ProCon.await ()
ConCon.signal ()
ConCon.await ()
ProCon.signal ()
/ / close the lock
Lock.unlock ()
Daemon thread
When the program calls the setDaemon method and sets the parameter to true, the current thread becomes a daemon thread. As long as the main thread ends, the daemon thread will end. This method must be called before start.
The join () method, which takes precedence over the main thread, waits for the current thread to execute; this method is after start.
At this point, I believe you have a deeper understanding of "Why should you let the run () method open automatically?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.