In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how Java communicates between two threads". In daily operation, I believe many people have doubts about how Java communicates between two threads. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how Java communicates between two threads". Next, please follow the editor to study!
In concurrent programming, the problem we must consider is how to communicate between two threads. Communication here refers to how information is exchanged between different threads.
At present, there are two ways:
Shared memory
Message passing (actor model)
# shared memory
Shared memory is a common way, and we often set a shared variable. Then multiple threads manipulate the same shared variable. In order to achieve the purpose of thread communication. For example, if we use multiple threads to perform page crawling tasks, we can use a shared variable count to record the number of tasks completed. Whenever a thread finishes crawling, it performs an add-1 operation on the original count. In this way, each thread can get the completion of the current task by getting the count variable. Of course, what must be considered is the synchronization of shared variables, which is why shared memory is prone to error.
In this communication model, there is no direct connection between different threads. They all interact through the "middleman" of shared variables. This "middleman" needs to be protected in the critical zone (locked or synchronized) if necessary. Thus, once the shared variables become more and more, and involves the interaction of a variety of different thread objects, this management will become very complex, prone to deadlocks and other problems.
Give a case
# messaging
Message delivery adopts direct communication between threads, and different threads explicitly send messages to achieve the purpose of interaction. The most famous way of message delivery is probably the actor model. In this model, everything is actor, and all communication between actor can only be achieved by passing messages. Each actor has an inbox (message queue) to hold messages received from other actor. Actor can also send messages to itself. This is the essence of object-oriented!
This model looks more complex than the shared memory model. But when it comes to complex business, the advantages of the actor model are realized. Let's take a multithreaded crawl website as an example to see how to solve it under this model.
First, we define a statistical actor to count the number of tasks completed. Then send multiple URLs (message method) to multiple crawling actor, and after crawling the actor, send a message to notify the completion of the actor task, and add one to the variable count saved by the statistical actor (which can only be seen by the statistical actor).
Finally, let's summarize these two communication modes:
The synchronization mechanism of concurrent model communication mechanism shares the common state of programs between memory threads, and the implicit communication synchronization between threads through writing-reading the common state in memory is explicit. The programmer must explicitly specify that a method or piece of code needs to be mutually exclusive between threads. There is no common state between actor threads, and threads must communicate explicitly by sending messages explicitly. Because the message must be sent before the message is received, the synchronization is implicit. At this point, the study on "how Java communicates between two threads" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.