multithreading - Java multi-threading programme not using a lot of CPU -
i beginner in programming , java, , first multi-core program. problem program never uses more 13% of cpu. not know if in right way or not.
how compute faster , use more cpu resources?
my program consists of 3 class:
the "main class instantiates work object number of threads
a "t1" class extends thread , contains work performed
a "work" class launches desired thread numbers , displays time taken threads perform work
here code of main
class:
public static void main(string[] args) { system.out.println("number of cpus available = " + runtime.getruntime().availableprocessors()); //display number of cpus available int iteration = 100000000; // define number of itterations threads /* instantiates each work different number of threads (1, 4, 8, 12, , 24) */ work t1 = new work(1); work t4 = new work(4); work t8 = new work(8); work t12 = new work(12); work t24 = new work(24); /* launch work each thread specified number of iterations */ t1.gowork(iteration); t4.gowork(iteration); t8.gowork(iteration); t12.gowork(iteration); t24.gowork(iteration); }
and here work class code:
public class work { static long time; // variable each thread increase time takes complete task. static int itterationperthread; // variable stores number of itterations per thread do. static int finish; // variable each thread incrase when finish task, used wait until thread has complete task. private int numberofthreads; // number of threads launch. /** * * constructor, set number of threads run * @param numberofthreads */ public work(int numberofthreads) { this.numberofthreads = numberofthreads; //set number of threads } /** * * method launch specified number of thread in constructor of class, , distributes number of iteration of each thread. * method nothing until each thread completes task , print time needed threads complete tasks. * @param itterationperthread */ public void gowork(int itterationperthread) { finish = 0; //reset variable in case call method more 1 time time = 0; //reset variable in case call method more 1 time this.itterationperthread = itterationperthread/numberofthreads; // divide given number of iterations number of threads specified in constructor (int i=0; i<numberofthreads; i++) //launch specified number of threads { new t1().run(); } while (finish != numberofthreads) //do nothing until thread completed task { } system.out.println("time " + numberofthreads + " thread = " + time + " ms"); //display total time } }
and t1 class:
public class t1 extends thread{ @override public void run() { long before = system.currenttimemillis(); (int i=0; i<work.itterationperthread; i++) //get thread busy number of itterations { math.cos(2.1545); //do something... } long after = system.currenttimemillis(); //compute elapsed time work.time += after - before; //increase static variable in work.java time elapsed thread work.finish++; // increase static variable in work.java when thread has finished job } }
the programme gives me following ouput on machine (four physical cores , 8 hyperthreaded):
number of cpus available = 8
time 1 thread = 11150 ms
time 4 thread = 4630 ms
time 8 thread = 2530 ms
time 12 thread = 2530 ms
time 24 thread = 2540 ms
according cpu result seems correct, cpu usage never exceeds 13%.
i found following stack overflow post, did not find answer question.
instead of calling thread.run()
, implements thread does, should call thread.start()
, create new thread , call run()
on new thread.
now running run()
on main thread, without making new thread. since have 13% cpu load, expect have 8 cores (meaning have filled single core).
even better create custom implementation of interface runnable
, instead of extending thread
. can run on thread follows:
thread t = new thread(new myrunnabletask()); t.start();
this common way because gives flexibility (later on) use more advanced mechanisms, such executorservice
.
edit: noted in of comments. changing same variables (the static ones in work
) several threads. should never this, because allows race conditions. instance incrementing variable can cause one, explained here.
Comments
Post a Comment