java - Overhead of threads on performance -


i'm trying increase performance of app adding threads concurrent tasks. results i've gotten confusing me , make me think there kind of thread related overhead of not aware. below 2 copies of same code exception 1 uses threads , other doesn't. 1 doesn't use threads runs 4 times faster 1 uses threads. i'm testing using device samsung note 4 quad processor. insights highly welcome.

thanks,

cwm

  public void testthreads() throws interruptedexception {   startmilli = system.currenttimemillis();   thread t1 = new thread() {         public void run() {             load1();         }     };      thread t2 = new thread() {         public void run() {             load2();         }     };     t1.start();     t2.start();     t1.join();     t2.join();   //  load1();   //  load2();     stopmilli = system.currenttimemillis();     diffmilli = stopmilli - startmilli;     startmilli = system.currenttimemillis(); } public void load1() {     list<integer> list1 = new arraylist<integer>();     for(i = 0; i<100000; i++) {         list1.add(i);     } }  public void load2() {     list<integer> list2 = new arraylist<integer>();     for(j = 100000; j<200000; j++){         list2.add(j);     } }    public void testthreads() throws interruptedexception {   startmilli = system.currenttimemillis();      load1();     load2();     stopmilli = system.currenttimemillis();     diffmilli = stopmilli - startmilli;     startmilli = system.currenttimemillis(); } public void load1() {     list<integer> list1 = new arraylist<integer>();     for(i = 0; i<100000; i++) {         list1.add(i);     } }  public void load2() {     list<integer> list2 = new arraylist<integer>();     for(j = 100000; j<200000; j++){         list2.add(j);     } } 

general guidelines:

it can quite fiddly increase application performance use of threads; few , you're not doing might, many , overhead of threads erodes benefits gain program's concurrency.

to right have setup many threads there cores, , make sure you're program breaks down nicely many threads. can tricky right if you're allowing program being run on wide range of hardware.

thread pools invented here. broadly speaking, have right number of threads hardware program happens running on, , let submit lots of small concurrent tasks execution without overhead of setting new thread each one. program runs on wide range of different hardware without having worry it.


Comments