Analysis of using instance of Java programming thread synchronization tool exchange

This paper mainly studies the use of Java programming thread synchronization tool exchange. Let's see the specific content below.

If two threads need to exchange each other's information, such as a data or used space, exchange needs to be used. Exchange provides a very convenient way for threads to exchange information. It can be used as the same step for two threads to exchange objects. Only when each thread enters the exchange () method and gives the object, To accept objects given when other threads return.

Only two threads can exchange data at a time. If there are multiple threads, only two threads can exchange data. Let's look at a popular example: hand in money and delivery!

From the code, I seem to see two people trading drugs... Let's see the trading results:

It runs fast. From the running results, it does realize the exchange of data. This is only the exchange of a basic type of data. Its real use is not limited to this. For example, we can exchange an object, which is useful. JDK officially mentioned an advanced application:

This has to be based on the actual situation. The idea is the same as the above. In practice, a buffer class must be defined, and then the class of the buffer is exchanged between the two threads. As for how to implement it in the class, it depends on the actual situation. So much for the use of exchange~

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>