只用数字制作唯一的ID?

发布于 2024-09-26 00:14:36 字数 466 浏览 0 评论 0原文

在一个程序中,我试图制作唯一的 ID 号。我用的是这样的:

AtomicInteger count = new AtomicInteger(0);
count.incrementAndGet();
int pid = count.get();
System.out.println("pid: " + pid);

但是我的教授是这样说的:

另一个问题是 pid 的生成。您所做的就是获取从 0 开始的下一个整数。当您到达进程表的末尾时会发生什么?您必须想出自己的 pid 生成算法,该算法根据项目规范工作,即它循环使用数字 0 到 99,然后当它循环回低数字时,它从最低的可用 pid 开始。有很多方法可以实现这样的算法,但最简单的方法是添加 1 mod 100,然后继续寻找,直到找到可用的 pid。当然,这意味着您必须跟踪哪些 pid 可用。

我该怎么做?

In a program, I am trying to make unique id numbers. I used this way:

AtomicInteger count = new AtomicInteger(0);
count.incrementAndGet();
int pid = count.get();
System.out.println("pid: " + pid);

But my professor said this:

Another problem is the pid generation. All you are doing is getting the next integer starting from 0. What happens when you reach the end of the process table? You had to come up with your own pid generation algorithm that works according to the project specification, i.e. it cycles through the numbers 0 through 99, and then when it cycles back to the low number it starts with the lowest available pid. There are many ways to implement such an algorithm, but the simplest thing to do is to add 1 mod 100, then keep looking until you find an available pid. Of course that means you have to keep track of which pids are available.

How do I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

影子的影子 2024-10-03 00:14:36

对我来说,你的教授的解释很清楚——其中有什么具体的部分是你不明白的吗?

如果您了解各个部分,请将任务分成更小的子任务并逐一实施。就像

  1. 从 0 到 99 循环数字一样(99 之后,下一个数字应该再次为 0),
  2. 添加一个工具来存储到目前为止生成的数字(例如,在 collection)
  3. 添加一个工具来检查当前编号是否尚未使用
  4. (添加一个工具来在关联进程终止时释放已使用的进程编号- 从描述中不清楚您是否需要这个。)

To me your professor's explanation is quite clear - is there any specific part in it that you don't understand?

If you understand the individual parts, divide the task into smaller subtasks and implement those one by one. Like

  1. cycle through the numbers from 0 to 99 (after 99, the next number should be 0 again)
  2. add a facility to store the numbers generated so far (e.g. in a collection)
  3. add a facility to check that the current number is not used yet
  4. (add a facility to free a used process number when the associated process terminates - it is not clear from the description whether or not you need this.)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文