flag = 1 ???
You are writing Java® not C. You have a boolean type which
you should use rather than messing around with 0=false and non‑0=true. Also flag is a very poor name for a flag variable; nobody knows what it means.
Why is that Map static? If you don't have a good reason to make something static, then it is a serious mistake.
Where are you using random numbers? The
thread asks abut random numbers but I can't see any random numbers anywhere.