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. Al开发者_开发百科l 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?
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
- cycle through the numbers from 0 to 99 (after 99, the next number should be 0 again)
- add a facility to store the numbers generated so far (e.g. in a collection)
- add a facility to check that the current number is not used yet
- (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.)
精彩评论