java - return a syncrhonized value from a syncrhonized block -



java - return a syncrhonized value from a syncrhonized block -

if have map of objects , each threads can go within each buckets concurrently,resulting block like

syncrhonized(values.get(objecttolock)){ int sum = 0; for(int = 0; i< new random().nextint(1000)+2; i++) sum+=i; homecoming sum; }

update

syncrhonized(values.get(objecttolock)){ int sum = 0; for(int = 0; i< new random().nextint(1000)+2; i++) sum+=i; for(int = 0; < 10; i++) for( int j = 0; j < 10 ; j++) system.out.println(*); homecoming sum; }

in piece of code, each thread can interfere between them resulting in sum distorted?

no, sum local variable. each thread have own copy.

declaring outside of synchronized block doesn't create less of local variable, makes scope larger.

java multithreading

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -