Inter-thread communication in concurrent programming

Inter-thread communication in concurrent programming

The goal of thread communication is to enable threads to send signals to each other. On the other hand, thread communication enables threads to wait for signals from other threads.

[[276788]]

Common ways of thread communication are:

  • wait/notify
  • Volatile Memory Sharing
  • CountDownLatch concurrency tool
  • Using ReentrantLock with Condition
  • Basic LockSupport implements blocking and waking up between threads

Method 1: Using the volatile keyword

The idea of ​​using shared memory to implement inter-thread communication based on the volatile keyword is to use multiple threads to monitor a variable at the same time. When the variable changes, the thread can sense and execute the corresponding business. This is also the simplest way to implement


The running results are:


Method 2: Use the wait() and notify() methods of the Object class

As we all know, the Object class provides methods for inter-thread communication: wait(), notify(), notifyaAl(), which are the basis of multi-threaded communication, and the idea of ​​this implementation is naturally inter-thread communication.

Note: wait and notify must be used with synchronized. The wait method releases the lock, while the notify method does not release the lock.


The result is


It can be seen from the screenshot of the printing result that after thread A sends a notify() wake-up notification, thread B still starts executing after completing the business of its own thread. This also shows that the notify() method does not release the lock, while the wait() method releases the lock.

Method 3: Use the JUC tool class CountDownLatch

After jdk1.5, many concurrent programming related tool classes are provided in the java.util.concurrent package, which simplifies the writing of our concurrent programming code. ***CountDownLatch*** is based on the AQS framework, which is equivalent to maintaining a shared variable state between threads


The running results are:


Method 4: Use ReentrantLock combined with Condition


The running results are:


Obviously, this method is not very good to use. The code is complicated to write, and thread B cannot execute immediately after being awakened by A because it has not acquired the lock. In other words, A does not release the lock after the awakening operation. This method is the same as Object's wait() and notify().

Method 5: Basic LockSupport to implement blocking and waking between threads

LockSupport is a very flexible tool for implementing blocking and waking up threads. When using it, you don't need to pay attention to whether the waiting thread or the waking thread runs first, but you need to know the name of the thread.


Operation Results

<<:  Two ways to decrypt HTTPS traffic with Wireshark

>>:  Understand HTTP and HTTPS protocols in ten minutes?

Recommend

Overcoming the Security Challenges of Software-Defined Networking

Today, more and more organizations are embracing ...

spinservers new VPS host 50% off, $7/month-2GB/20G SSD/1TB/San Jose data center

spinservers launched a new VPS host product this ...

Breaking news! 5G standard postponed for 3 months

According to reliable intelligence, at the 3GPP m...

A thorough investigation of the history behind Huawei's high-quality Wi-Fi ONTs

[51CTO.com original article] Only after careful c...

How is the world's largest OpenRAN operator doing?

On February 14, Japanese operator Rakuten Mobile ...

How will the network be reconstructed in the 5G era?

The 5G era is approaching. While people are full ...

IDC: Enterprise WLAN market grew strongly in the third quarter

The global consumer and enterprise wireless LAN (...

Three steps to take before deploying SD-WAN

As enterprises develop their network strategies a...

8 predictions for the development of network technology in 2017

The Internet is evolving at an unprecedented pace...