In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how Java uses Exchanger to achieve the exchange of equipment in the game, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.
What is 1 Exchanger?
JDK 1.5 begins with the Exchanger class provided under the JUC package that can be used to exchange information between two threads. The Exchanger object can be understood as a container containing two grids, which can be filled with information by calling the exchanger method. When the information in the two grids is filled, the information in the two grids is automatically exchanged, and then the exchanged information is returned to the calling thread, thus realizing the information exchange between the two threads.
The function seems simple, but it is useful in some scenarios, such as the exchange of equipment between two players in the game, and the matching of male and female lovers in the dating software.
The following is a simple simulation of the scene in which the two players exchange equipment.
Package com.chenpi;import java.util.concurrent.Exchanger;/** * @ Description * @ Author tangerine peel * @ Date 2021-7-11 * @ Version 1.0 * / public class ChenPiMain {public static void main (String [] args) throws InterruptedException {Exchanger exchanger = new Exchanger (); new Thread ()-> {String str = null; try {str = exchanger.exchange ("Dragon Slayer") } catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("deal succeeded" + Thread.currentThread (). GetName () + "get" + str);}, "Zhou Zhi Ruo") .start (); new Thread ()-> {String str = null; try {str = exchanger.exchange ("rely on Tianjian") } catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("deal succeeded" + Thread.currentThread (). GetName () + "get" + str);}, "Zhang Wuji") .start ();}}
The output is as follows:
The deal was successful, and Zhang Wuji won the dragon knife.
If the transaction is successful, Zhou Zhi gets to rely on Tianjian.
2 Exchanger detailed explanation
The Exchager class can be used to exchange information between two threads. If one thread calls the exchange method of the Exchanger object, it blocks until the other thread exchanges information with it, and the information after the exchange is returned to the calling thread, thus realizing the information exchange between the two threads.
Spin and cas mechanisms are also used in the underlying layer of Exchager.
Note: if more than two threads call the same Exchanger object exchange method, the result is unpredictable, as long as two threads meet the condition, the match is considered successful and the information is exchanged. The remaining threads that cannot be paired will be blocked and wait until another thread can match it.
Package com.chenpi;import java.util.concurrent.Exchanger;/** * @ Description * @ Author tangerine peel * @ Date 2021-7-11 * @ Version 1.0 * / public class ChenPiMain {public static void main (String [] args) {Exchanger exchanger = new Exchanger (); new Thread (()-> {String str = null; try {str = exchanger.exchange) } catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("deal succeeded" + Thread.currentThread (). GetName () + "get" + str);}, "Zhou Zhi Ruo") .start (); new Thread ()-> {String str = null; try {str = exchanger.exchange ("rely on Tianjian") } catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("deal succeeded" + Thread.currentThread (). GetName () + "get" + str);}, "Zhang Wuji") .start (); new Thread ()-> {String str = null; try {str = exchanger.exchange ("fake relying on Heavenly Sword") } catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("deal succeeds," + Thread.currentThread (). GetName () + "get" + str);}, "Cheng Kun"). Start ();}} / / the output is as follows: if Zhou Zhi gets a fake Tianjian deal successfully, Cheng Kun gets a dragon slaughter sword.
Of course, the thread waiting for the exchange of information can be interrupted, for example, when the player is waiting for the transaction, suddenly the player goes offline, then the thread should be interrupted to wait.
Package com.chenpi;import java.lang.Thread.State;import java.util.ArrayList;import java.util.List;import java.util.concurrent.Exchanger;/** * @ Description * @ Author tangerine peel * @ Date 2021-7-11 * @ Version 1.0 * / public class ChenPiMain {public static void main (String [] args) throws InterruptedException {Exchanger exchanger = new Exchanger (); List threads = new ArrayList (3); Thread thread1 = new Thread (()-> {String str = null) Try {str = exchanger.exchange ("Dragon Slayer");} catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("deal succeeded" + Thread.currentThread (). GetName () + "get" + str);}, "Zhou Zhiruo"); threads.add (thread1); Thread thread2 = new Thread (()-> {String str = null) Try {str = exchanger.exchange ("relying on Heavenly Sword");} catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("deal succeeded" + Thread.currentThread (). GetName () + "get" + str);}, "Zhang Wuji"); threads.add (thread2); Thread thread3 = new Thread (()-> {String str = null) Try {str = exchanger.exchange ("fake dragon sword");} catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("deal succeeded" + Thread.currentThread (). GetName () + "get" + str);}, "Chengkun"); threads.add (thread3); for (Thread thread: threads) {thread.start () } / / wait 5 seconds Thread.sleep (5000); for (Thread thread: threads) {System.out.println (thread.getName () + ":" + thread.getState ()); / / interrupt thread if (thread.getState () = = State.WAITING) {thread.interrupt () if you are still blocking waiting } / / the output result is as follows: Zhang Wuji is successful in trading, Zhang Wuji is successful in trading dragon swords, Zhou Zhi if Zhou Zhi is reliant on Tianjian Zhou Zhi Ruo: TERMINATED Zhang Wuji: TERMINATED Cheng Kun: WAITING transaction is successful, Cheng Kun gets nulljava.lang.InterruptedException at java.util.concurrent.Exchanger.exchange (Exchanger.java:568) at com.chenpi.ChenPiMain.lambda$main$2 (ChenPiMain.java:47) at java.lang.Thread.run (Thread.java:748)
The above example shows that a thread will wait forever if it cannot wait for another thread to exchange information with it. In fact, Exchanger can also set a specified waiting time. For example, the system sets the matching time of the player exchange equipment to 60 seconds, and if the time is exceeded, the transaction will be terminated.
Package com.chenpi;import java.util.concurrent.Exchanger;import java.util.concurrent.TimeUnit;import java.util.concurrent.TimeoutException;/** * @ Description * @ Author tangerine peel * @ Date 2021-7-11 * @ Version 1.0 * / public class ChenPiMain {public static void main (String [] args) {Exchanger exchanger = new Exchanger () New Thread (()-> {try {/ / timeout is set to 5 seconds String str = exchanger.exchange ("Dragon Slayer", 5, TimeUnit.SECONDS); System.out.println ("deal succeeded," + Thread.currentThread (). GetName () + "get" + str);} catch (TimeoutException e) {System.out.println ("transaction timeout!") ; e.printStackTrace ();} catch (InterruptedException e) {System.out.println ("abnormal termination of the transaction"); e.printStackTrace ();}}, "Zhou Zhi Ruo"). Start ();}} / / output the following transaction timeout! Java.util.concurrent.TimeoutException at java.util.concurrent.Exchanger.exchange (Exchanger.java:626) at com.chenpi.ChenPiMain.lambda$main$0 (ChenPiMain.java:22) at java.lang.Thread.run (Thread.java:748) 3 Exchanger application
Exchager is very useful in genetic algorithm and pipeline design. For example, if a buffer is exchanged between two threads, the thread that populates the buffer gets a freshly emptied buffer from another thread when needed, and passes the filled buffer to the thread that empties the buffer.
Package com.chenpi;import java.awt.image.DataBuffer;import java.util.concurrent.Exchanger;/** * @ Description * @ Author tangerine peel * @ Date 2021-7-11 * @ Version 1.0 * / public class ChenPiMain {Exchanger exchanger = new Exchanger (); DataBuffer initialEmptyBuffer =. A made-up type DataBuffer initialFullBuffer =... Class FillingLoop implements Runnable {public void run () {DataBuffer currentBuffer = initialEmptyBuffer; try {while (currentBuffer! = null) {addToBuffer (currentBuffer); if (currentBuffer.isFull ()) {currentBuffer = exchanger.exchange (currentBuffer) } catch (InterruptedException ex) {... handle...} class EmptyingLoop implements Runnable {public void run () {DataBuffer currentBuffer = initialFullBuffer; try {while (currentBuffer! = null) {takeFromBuffer (currentBuffer); if (currentBuffer.isEmpty ()) {currentBuffer = exchanger.exchange (currentBuffer) } catch (InterruptedException ex) {... handle...} void start () {new Thread (new FillingLoop ()) .start (); new Thread (new EmptyingLoop ()) .start () }} Thank you for reading this article carefully. I hope the article "Java how to use Exchanger to exchange equipment in the game" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you 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.