For some academic research I need to simulate several threads running on a single processor.
I want to be able to insert *call_scheduler()* calls inside my code, in which the current "thread" will pause (remembering in which code line it is) and some scheduling function will decide which thread to let go.
In python, this could be implemented neatly using stackless python. Is there a java alternative?
I could implement it using real threads and some messaging queues (or pipes) that will force only one thread to run at a time - but this is an ugly and problematic solution.
Your question:
I could implement it using real threads and some messaging queues (or pipes)
that will force only one thread to run at a time- but this is an ugly and problematic solutionWell if you want only a single thread to run at a time, by controlling the access of the thread on the object in a cleaner way, then use the
Semaphores in java.util.concurrent package.Semaphores sem = new Semaphores(1);// 1 here will mark that only one thread can have accessuse
sem.acquire() to get the key of the object, and when its done, use sem.release()then only another thread will get the access to this object.