That would depend on how you are generating said code. If you do it with some method, then you can synchronize that method.
private int code = 0;
public synchronized int getCode()
{
return ++code;
}
But then you have to make absolutely sure that this method is the ONLY way you fetch the code.