This article shares the specific code for Java to implement the red envelope grab function for your reference. The specific content is as follows
Key ideas:
1. Grabbing red envelopes involves concurrent operations for multiple people, and synchronization is required to ensure that the multi-threaded operation results are correct.
2. Due to the large number of people online at the same time, from the perspective of performance, players do not need to respond in a timely manner to send red envelope requests, but the server will execute the red envelope queue regularly.
The following is the main code and implementation logic description
1. Create a class to represent the entity concept of red envelopes. Directly use atomic variables to ensure the synchronization of increase and decrease. Java's atomic variables are a more fine-grained synchronization mechanism. In highly competitive situations, the performance of the lock will exceed the performance of the atomic variables, but in more realistic competition situations, atomic variables enjoy better performance.
public class SpringGift { private String role; private AtomicInteger gift; public String getRole() { return role; } public void setRole(String role) { this.role = role; } public AtomicInteger getGift() { return gift; } public void setGift(AtomicInteger gift) { this.gift = gift; } public int getRemainCount(){ return this.gift.get(); }} 2. Use multi-threading simulation to grab red envelopes at the same time. The server saves the red envelopes sent by the player in a queue, and then uses a job to push the red envelope information to the player regularly. The red envelope grab requests for each batch of players are actually operated by the first red envelope element popped up from the queue, but when the current number of red envelopes is empty, the next red envelope will automatically pop up (if any).
public class Test { public static ConcurrentLinkedQueue<SpringGift> queue; public static SpringGift currGift; public static AtomicInteger count = new AtomicInteger(); static class myThread implements Runnable{ public void run(){ handleEvent(); } } public static void main(String[] args) throws Exception { queue = new ConcurrentLinkedQueue<SpringGift>(); for(int i =0;i<3;i++){ SpringGift gift = new SpringGift(); gift.setRole("role"+i); gift.setGift(new AtomicInteger(50)); queue.add(gift); } myThread mythread = new myThread(); for(int i=0;i<1000;i++){ new Thread(mythread).start(); } System.err.println("Received in total"+count.get()); } private static SpringGift getGift(){ //Prevent multiple threads from popping out the queue at the same time synchronized (queue) {//If there is no lock, the total number of printed counts is incorrect! ! ! ! if(currGift == null || currGift.getRemainCount() <=0){ currGift = queue.poll(); } } return currGift; } public static void handleEvent(){ try{ SpringGift obj = getGift(); if(obj == null || obj.getRemainCount() <= 0){ System.err.println("Not"); return ; } if(obj !=null && obj.getGift().getAndDecrement() >0 ){ System.err.println("Get a red envelope"); count.getAndIncrement(); } Thread.sleep(500);//Simulate and process other operations}catch(Exception e){ e.printStackTrace(); } }}The screenshot of the running results is as follows
It should be noted that since the getGift() method automatically pops up the header element, the synchronization mechanism must be done. Otherwise, when multiple requests operate the last remaining of a certain red envelope at the same time, the total number of red envelopes will be incorrect.
(After commenting on the locked code, you will find that the total number of printed may be incorrect!)
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.