java - Adjust Fairness in Multithreading -


how can adjust fairness between given k threads generate output?
in other words imagine have k threads printing "1" , n threads printing "2".
how can put fairness between threads each thread print(for example "1") as other (k - 1) print(for example "1").and same n thread printing "2".

before create threads, create array[0..numthreads-1] of empty semaphores, 1 each thread going create. signal each thread on creation incrementing semaphore index, 0..numthreads-1.

in thread function, have wait on semaphore[index], print something, signal [(index+1) mod numthreads] semaphore, loop round wait on semaphore[index] again.

once have done that, nothing should happen @ all.

throw in 1 semaphore unit, anywhere.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -