gpt4 book ai didi

java - 仅使用数字制作唯一 ID?

转载 作者:行者123 更新时间:2023-11-29 06:19:12 26 4
gpt4 key购买 nike

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

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

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

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.

我该怎么做?

最佳答案

我觉得你教授的解释很清楚-其中有什么具体部分你不明白吗?

如果您了解各个部分,请将任务分成更小的子任务,然后一个一个地执行。喜欢

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

关于java - 仅使用数字制作唯一 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3846327/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com