简体   繁体   English

寻找在多线程平台上创建用户名的好方法

[英]Looking for a good way to create usernames on multithreaded platform

I currently have a set of rules to create usernames based on a user's initials. 我目前有一套规则来根据用户的姓名首字母创建用户名。 This is not adjustable. 这是不可调整的。 So a user has 'xxx' as his initials. 因此用户将'xxx'作为首字母缩写。 His username would be zzxxx1. 他的用户名是zzxxx1。 The next user with the same initials would be zzxxx2 and so on. 具有相同首字母的下一个用户将是zzxxx2,依此类推。 I'm using Java to generate the ids (useridCreation.jar). 我正在使用Java来生成id(useridCreation.jar)。 This jar is being called by a multi-threaded application (ITIM). 这个jar由多线程应用程序(ITIM)调用。 So how would I go about reserving zzxxx1 until the first user is finished being created so as to not have duplicate usernames. 那么我将如何在第一个用户完成创建之前保留zzxxx1,以便不会有重复的用户名。 This is a feed type situation where the usernames are being generated. 这是生成用户名的订阅源类型情况。 I can pull up to 4 users/sec. 我最多可以达到4个用户/秒。 I was thinking Vector, but I'm not too familiar with multi-threaded applications so I'm not sure how to approach that. 我在想Vector,但我不太熟悉多线程应用程序,所以我不确定如何处理它。 SQL table is another option, but I feel like even with concurrent writing off, there would be some issues there. SQL表是另一种选择,但我觉得即使同时注销,也会出现一些问题。 What about a file that I lock/unlock. 我锁定/解锁的文件怎么样? Has anybody implemented a solid solution for this type of issue? 有没有人为这类问题实施了可靠的解决方案?

Edit: I forgot to mention this is a cluster environment. 编辑:我忘了提到这是一个集群环境。

This jar is being called by a multi-threaded application (ITIM). 这个jar由多线程应用程序(ITIM)调用。 So how would I go about reserving zzxxx1 until the first user is finished being created so as to not have duplicate usernames. 那么我将如何在第一个用户完成创建之前保留zzxxx1,以便不会有重复的用户名。

If this is a cluster configuration (ie distributed applications running on multiple servers) then you are going to have to have some sort of central service that guarantees the uniqueness of the names. 如果这是一个集群配置(即在多个服务器上运行的分布式应用程序),那么您将不得不拥有某种中心服务来保证名称的唯一性。 One easy solution would be a database with unique constraints on the username field in some table. 一个简单的解决方案是在某些表中对用户名字段具有唯一约束的数据库。 Each server could do something like an ordered LIKE query to find the highest username number and then try to create an entry using the next number. 每个服务器可以执行类似于有序LIKE查询的操作以查找最高用户名编号,然后尝试使用下一个编号创建条目。 If it was taken it would try again with the next number... 如果它被采取它将再次尝试与下一个号码...

Another solution would be to have a central service which does the uniqueness guarantee. 另一种解决方案是拥有一个能够保证唯一性的中央服务。 All of the cluster nodes would contact the central one which would use a synchronized or other lock to ensure that only one thread is generating a unique name at a time. 所有群集节点都将联系中心群集,该群集节点将使用synchronized或其他锁定以确保一次只有一个线程生成唯一名称。 Then again I assume you need persistence so maybe the database is the easiest option. 然后我再次假设您需要持久性,因此数据库可能是最简单的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM