只用数字制作唯一的ID?
在一个程序中,我试图制作唯一的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我来说,你的教授的解释很清楚——其中有什么具体的部分是你不明白的吗?
如果您了解各个部分,请将任务分成更小的子任务并逐一实施。就像
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