Java multithreading - reader and writer thread in synchronized block -
this question job interview:
how identify read thread , write thread in synchronized block?
you can like:
thread current = thread.currentthread()
and now; when have map/list/... of threads, can compare references. simple example:
you add 2 fields class:
private thread reader = private thread writer =
and do
synchronized foo() { if (thread.currentthread() == reader) ...
and record: although things easy, person dealing "this problem" should rather step back: smells xy problem on place.
meaning: in "real" world; consider code bad practice. likely, tries solve problem should solved in other ways!
so, answer interviewer better combination of direct technical answer; pointing out "bad practice" issue.
Comments
Post a Comment