Alex Rivera | Logout

How can I synchronize getter while a setter is working in Java

Asked 2011-11-19T16:37:19.997
9

I am having a multi-thread application using a single static class which provides a list. I want getters of the static class to work freely (not synchronized against each other) but when a setter is working I want all getters to get locked and wait until setter's job is done. I don't want to lock getters when they are called together since it would degrade performance a lot. Getters are called 1,000,000 times per day and setter is only supposed to work once per day.

Edit
Report

1 Answer

1

This is the archetypal case to use a CopyOnWriteArrayList.

"This is ordinarily too costly, but may be more efficient than alternatives when traversal operations vastly outnumber mutations, and is useful when you cannot or don't want to synchronize traversals, yet need to preclude interference among concurrent threads. "

answered 2011-11-19T17:15:30.787

Your Answer