Sunday 17 May 2015

Thread Synchronization strategy


Requirement

Two thread wants to make sure they don't run if other thread is running.

Simple approch

1. Both work on single object
2. Wait on the object property, like "isAvailable"
3. call notify once they complete the task.

But what if the thread never reset the condition ?


Approch second

1. Ask every one to pass "Task" object
2. call task object runTask()
3. every thread have some inner class of seperate myTask implements Task interface and overrides runTask()
4. Put the main task, which requires exclusive lock, inside this method.

Issue:
Forces every one to follow this framework, implement interface etc

No comments:

Post a Comment