How to save a name pool?

I need to keep a list of users (proxy accounts) that will be transferred to multi-threaded clients. Mostly clients will use user identifiers to perform actions; but for this question it doesnโ€™t matter what these actions are. When a client receives a user ID, it is not available to other clients until the action is complete.

I am trying to think of a parallel data structure to support this user pool. Any ideas? Will ConcurrentQueue do the job? Clients will deactivate the user ID and add back the user ID when they are finished with it.

+4
source share
4 answers

In a similar situation, I implemented my own BlockingQueue . While you are tracking the identity pool, you can close the queue at the appropriate time.

+2
source

Do you really need to have a limited user pool? Otherwise, each user may simply have an increasing user ID. Thus, there is no risk of a collision, and it seems to me much easier.

0
source

I would use BlockinQueue .

This will allow client threads to poll identifiers (until one is available or the time limit is exceeded) and click them after use.

0
source

Does BlockingQueue match? What actions should be taken if the Queue is empty? It looks like you probably have a limited number of connections to your third-party software. In this case, you can display an explanatory message to any user who cannot obtain the user ID. In this case, locking would not be useful. You could just implement some synchronization around the standard queue and handle the empty queues as you see fit. Something like that?

public class UserIDQueue { private static final Queue<String> USER_IDS = loadUserIDs(); private static final Object USER_ID_LOCK = new Object(); //doesn't need to be synchronized as it is called at class load time private static Queue<String> loadUserIDs() { Queue<String> userIDs = new LinkedList<String>(); for(String userID : THIRD_PARTY_USER_IDS) { userIDs.add(userID); } return userIDs; } public static String getNextUserID() { synchronized(USER_ID_LOCK) { String userID = USER_IDS.poll(); if (userID == null) { //not sure what your logic is, probably an exception } return userID; } } public static void returnUserID(String userID) { synchronized(USER_ID_LOCK) { USER_IDS.add(userID); } } 

}

0
source

Source: https://habr.com/ru/post/1308106/


All Articles