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

What is java multithreading

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly talks about "what is java multithreading". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is java multithreading"?

Thread declaration cycle

The five states of a thread: new, ready, run, blocking, and dead. In which the ready and running state customers switch to each other, but run to blocking, blocking to ready, only one-way transition.

The thread that just came out of new is in a [new] state. After calling start, it is in a [ready] state. After obtaining CUP resources and executing it, it is [running] state. All these behaviors such as CUP running out / sleep () / call blocking IO/ suspended by suspend () / getting other thread synchronization monitors / waiting for notification may make the thread enter [blocking] state from [run].

Three ways of creating Thread and their comparison

Inherit the Thread class to create a thread

You need to write a subclass of Thread and override the thread executor run () method to start the thread through the start () method.

Implement the Runable interface to create a thread class

You need to override the run () method in the implementation class, and the instance of the implementation class will be the target object, starting the thread through new Thread (target, "thread name"). Start ().

Use the Callable interface with the Future interface

The call () method in Callable acts as the thread execution body, and the instance of the implementation class FutureTask of Future (which also implements Runable) is used as target, the Callable instance is passed in when initializing FutureTask, and the thread is started through new Thread (target, "thread name"). Start ().

Comparison of advantages and disadvantages

Programming using the method of inheriting Thread classes is relatively simple, but you can no longer inherit other classes, and variables cannot be shared among multiple threads.

Using the Runable interface method, you can inherit other classes at the same time, but you cannot get the return value of the thread or throw an exception.

Although the programming method of Callable combined with Future is more complex, it is not limited by inheritance, it can get the return value of multi-thread, and it can throw an exception, so it is the most commonly used method to realize multi-thread.

Control thread

Join () waits for another thread: calling the join () method of another thread in one thread will cause the current thread to block until it has been executed by the join thread. The join () method is usually called in the main thread, which ensures that all child threads end before the main thread ends.

SetDaemon (): the setDatemon () that calls the thread itself before start () can be set to the background thread: if all foreground threads die, the background thread will die automatically.

Sleep (): the current thread can be put into a [sleep] state through Thread.sleep (). Until sleep time arrives, even if a CUP is available, the thread cannot execute, and the waking thread can only enter the [ready] state.

Yield (): through Thread.yield (), you can set the priority of a thread (with numbers or constants) to put the thread into a [ready] state, which is equivalent to pausing the thread. Only threads with a priority equal to or higher than the current thread can get CUP resource execution, otherwise the current thread will continue to execute.

Thread synchronization

To solve the classic producer-consumer problem (to ensure the atomicity of transactions, for example, multiple threads deposit money from the same account, saving and withdrawing each need to be an atomic operation).

The method of thread synchronization

Synchronize code blocks. The code that needs to be synchronized is placed in curly braces in synchronized (obj) {}, so that the thread needs to obtain the synchronization monitor before executing the synchronization block, and after execution, the synchronization monitor needs to be released.

Synchronization method. Using synchronized to decorate the entire method (not the static method), the synchronization monitor is this, and when this method is called in the thread execution body run () or call (), the thread that needs to acquire the lock of the synchronization monitor (that is, an instance of the class where the synchronization method resides) can execute the synchronization method.

Note that the sleep (), yield (), suspend () methods do not release the synchronization monitor.

Sync lock. The most commonly used ReentrantLock lock is a more flexible way of synchronization, where threads need to be locked first, followed by successful threads to execute code, and then unlocked. Use try... Finally guarantees lock release.

Deadlock

The two threads wait for each other (release the synchronization monitor) and enter a deadlock.

For example, thread 1 locks object A, then sleeps, thread 2 locks object B, and then thread A wakes up. If thread An asks for a B object monitor at this time, A will be blocked. If B wakes up and requests A monitor, B will also block, and An and B will each hold a monitor and request each other's monitor at this time, will enter a deadlock.

Thread communication

Thread communication ensures the order in which threads are executed.

There are usually three ways to communicate

Traditional thread communication, using wait (), nofity (), nitifyAll (), is suitable for synchronous threads using synchronous code blocks and synchronization methods.

Use the condition object returned by the lock object to control thread communication. Condition also contains three methods, await (), signal (), and signalAll (). The condition method is used in situations where threads are synchronized with using lock.

Use blocking queues (BlockingQueue) to control thread communication. Its characteristic is that when the queue is full, the producer thread blocks, and only the consumer thread can execute the code; when the queue is empty, the consumer thread will block, and only the producer thread can execute the code. When the thread is not empty and not full, both the producer and the consumer can execute. This approach is more flexible and can control thread switching through queue capacity.

At this point, I believe you have a deeper understanding of "what is java multithreading", you might as well come to the actual operation of it! 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.

Share To

Internet Technology

Wechat

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

12
Report