import java.io.*;
class threads extends Thread{
int counter=0;
static volatile boolean counting=true;
public void run(){
if(counting) {
counter++;
}
}
}
public class cores {
static PrintWriter pw= new PrintWriter(System.out, true);
@SuppressWarnings({ "static-access", "deprecation" })
public static void main(String args[]) throws InterruptedException {
int mastercounter=0;
Thread mainthread=Thread.currentThread();
int cores= Runtime.getRuntime().availableProcessors();
threads[] array=new threads[cores];
for (int x=0;x<cores;x++) {
threads t=array[x];
t.start();
}
mainthread.sleep(5000);
threads.counting=false;
for (int x=0;x<cores;x++) {
threads t=array[x];
t.stop();
mastercounter+=t.counter;
}
pw.println(mastercounter);
}
The code above is my attempt at a simple benchmark program that tries to take advantage of all cores available on the CPU of a computer by simply using all the threads possible to count in 5 seconds.
The code gives a NullPointerException
on the line t.start()
which I assume is because of the way I tried to work around not being able to name variables dynamically.
Any advice, fixes or tips are much appreciated.