reduce in performance when used multithreading in java -
i new multi-threading , have write program using multiple threads increase efficiency. @ first attempt wrote produced opposite results. here have written:
class threadimpl implements callable<arraylist<integer>> { //bloom filter instance 1 of table bloomfilter<integer> bloomfilterinstance = null; // data member complete data access. arraylist< arraylist<userbean> > data = null; // store result of testing arraylist<integer> result = null; int tableno; public threadimpl(bloomfilter<integer> bloomfilterinstance, arraylist< arraylist<userbean> > data, int tableno) { this.bloomfilterinstance = bloomfilterinstance; this.data = data; result = new arraylist<integer>(this.data.size()); this.tableno = tableno; } public arraylist<integer> call() { int[] tempresult = new int[this.data.size()]; for(int i=0; i<data.size() ;++i) { tempresult[i] = 0; } arraylist<userbean> chkdataset = null; for(int i=0; i<this.data.size(); ++i) { if(i==tableno) { //do nothing; } else { chkdataset = new arraylist<userbean> (data.get(i)); for(userbean tochk: chkdataset) { if(bloomfilterinstance.contains(tochk.getuserid())) { ++tempresult[i]; } } } this.result.add(new integer(tempresult[i])); } return result; } }
in above class there 2 data members data
, bloomfilterinstance
, they(the references) passed main program. there 1 instance of data , bloomfilterinstance , threads accessing simultaneously.
the class launches thread is(few irrelevant details have been left out, variables etc. can assume them declared):
class multithreadedvrsion { public static void main(string[] args) { if(args.length > 1) { executorservice es = executors.newfixedthreadpool(nooftables); list<callable<arraylist<integer>>> threadedbloom = new arraylist<callable<arraylist<integer>>>(nooftables); (int i=0; i<nooftables; ++i) { threadedbloom.add(new threadimpl(eval.bloomfilter.get(i), eval.data, i)); } try { list<future<arraylist<integer>>> answers = es.invokeall(threadedbloom); long endtime = system.currenttimemillis(); system.out.println("using more 1 thread bloom filters: " + (endtime - starttime) + " milliseconds"); system.out.println("**printing results**"); for(future<arraylist<integer>> element: answers) { arraylist<integer> arrint = element.get(); for(integer i: arrint) { system.out.print(i.intvalue()); system.out.print("\t"); } system.out.println(""); } } catch (exception e) { e.printstacktrace(); } } } }
i did profiling jprofiler ,
![here]:(http://tinypic.com/r/wh1v8p/6)
is snapshot of cpu threads red color shows blocked, green runnable , yellow waiting. problem threads running 1 @ time not know why?
note:i know not thread safe know doing read operations throughout , want analyse raw performance gain can achieved, later implement better version.
can please tell have missed
one possibility cost of creating threads swamping possible performance gains doing computations in parallel. can't tell if real possibility because haven't included relevant code in question.
another possibility have 1 processor / core available. threads run when there processor run them. expectation of linear speed number of threads , possibly achieved (in theory) if free processor each thread.
finally, there memory contention due threads attempting access shared array. if had proper synchronization, potentially add further contention. (note: haven't tried understand algorithm figure out if contention in example.)
my initial advice profile code, , see if offers insights.
and take @ way measuring performance make sure aren't seeing benchmarking artefact; e.g. jvm warmup effects.
Comments
Post a Comment