Friday, 15 May 2015

java - Multi-threading -- a faster way? -



java - Multi-threading -- a faster way? -

i have class getter getint() , setter setint() on field, field

integer int;

of object of class, someclass.

the setint() here synchronized-- getint() isn't.

i updating value of int within multiple threads. each thread getting value int, , setting appropriately. threads aren't sharing other resources in way.

the code executed in each thread follows.

public void update(someclass c) { while (<condition-1>) // conditions here , calculation of // k below dont have // members of c if (<condition-2>) { // calculate k here synchronized (c) { c.setint(c.getint()+k); // system.out.println("in "+this.tostring()); } } }

the run() method invoking above method on members updated within constructor params passed it:

public void run() { update(c); }

when run on big sequences, threads aren't interleaving much-- see 1 thread executing long without other thread running in between.

there must improve way of doing this.

i can't alter internals of someclass, or of class invoking threads.

how can done better?

tia.

//=====================================

edit:

i'm not after manipulating execution sequence of threads. have same priority. it`s see in outcome suggesting threads aren't sharing execution time evenly-- 1 of them, 1 time takes over, executing on. however, can't see why code should doing this.

it`s see in outcome suggesting threads aren't sharing execution time evenly

well, don't want if after efficiency. yanking thread beingness executed , scheduling thread costly. hence it's advantageous one of them, 1 time takes over, executing on. of course, when overdone see higher throughput longer response time. in theory. in practice, jvms thread scheduling tuned purposes, , don't want seek changing in situations. rule of thumb, if interested in response times in millisecond order, want remain away messing it.

tl;dr: it's not beingness inefficient, want leave is.

edit: having said that, using atomicinteger may help in performance, , in sentiment less error prone using lock (synchronized keyword). need hitting variable hard in order measurable benefit though.

java multithreading time

No comments:

Post a Comment