Multi-threaded java program preventing cpu from monitoring its heat

Can a java program use all available threads and cpu power until the cpu no longer detects its own heat?

I was working on a small project for educational purposes when I came across strange behavior that I did not know was possible via Java. To learn a little about multithreading, thread-safe and efficient math calculations, I made a quick and dirty program to find prime numbers.

Problems started when I set the thread pool size to 12 and used all threads (6 core I7 with hyperthreads, specs will be lower), I found that my cpu was not responding to its own growth rate, only when I stopped the program noticed again and began to deploy fans.

Hardware Features:

  • CPU: (6 cores, 12 threads) I7-5820k @ 3.3 GHz
  • Motherboard: Asus X99-DELUX
  • CPU-Fan: NZXT Kreaken x61

Measurement software used:

  • NZXT CAM
  • CPUZ

Some tests I ran to see what and why it is: Ofc I needed to establish that this was actually happening, so I opened the Process Manager, CPU-Z and CAM to monitor CPU usage and high temperatures, and of course when I ran the code, everything just froze and stayed the same (about 35 C), but as soon as I stopped it the pace shot up to 70 degrees.
Performing the same test with just 10 threads, leaving 2 stages intact, the fans gradually increased with temperature.

There are 3 main classes in this program:

Stream Safe public class SyncedLong {private long val;

    public SyncedLong(long val){
        this.val = val;
    }

    public synchronized void increment(){
        val++;
    }

    public synchronized long incrementAndGet(){
        val++;
        return val;
    }

    public synchronized long get(){
        return val;
    }
}

      

My ThreadControler class for storing and handling active threads:

public class ThreadControler {

    ArrayList<PrimeCalculatorThread> primePool = new ArrayList<>();
    ExecutorService executor = Executors.newFixedThreadPool(10);

    public void increment() {
        if(executor.isTerminated()){executor = Executors.newFixedThreadPool(12);} //12 is the amount of threads on my cpu
        PrimeCalculatorThread pct = new PrimeCalculatorThread();
        primePool.add(pct);
        executor.execute(pct);

    }

    public void decrement() {
        PrimeCalculatorThread pct = primePool.get(primePool.size()-1);
        pct.terminate();
        primePool.remove(pct);
    }

    public void terminate() {
        for(PrimeCalculatorThread pct : primePool){
            pct.terminate();
        }
        primePool.clear();
        executor.shutdown();
    }
}

      

Finally, the cruncher number, PrimeCalculatorThread

public class PrimeCalculatorThread implements Runnable {
    // 77263
    public static SyncedLong threaddedLong = new SyncedLong(0); //Startes a new SyncedLong at starting number 0.
    long currentNumber = 0;
    long lastNumber = 0;
    long a = 0;
    long b = 0;
    long c = 0;
    long startingNumber = 0;
    boolean foundPrime;
    boolean running = true;

    @Override
    public void run() {
        while (running) {
            currentNumber = threaddedLong.incrementAndGet();
            foundPrime = true;

            if (currentNumber % 2 != 0) {
                a = 0;
                while (a <= currentNumber) {
                    b = a;
                    while (b <= currentNumber) {
                        c = a * b;
                        if (a != currentNumber && b != currentNumber) {
                            if (c == currentNumber) {
                                foundPrime = false;

                            }
                        }
                        b++;
                    }
                    a++;
                }
                if (foundPrime) {
                    Output.println("Found prime: " + currentNumber + "    Difference: " + (currentNumber - lastNumber) + "    " + Thread.currentThread().getName());
                    lastNumber = currentNumber;
                }
            }
        }
    }

    public void terminate() {
        running = false;
    }
}

      

CPU usage CPU usage


English is not my native language, so don't judge my wording and grammar too harshly, just post a quick question if you feel like something is wrongly phrased or if it just doesn't make enough sense (if any).

+3


source to share


1 answer


Intel's current technology is so advanced. Even if you think you are using all the cores and the thread at the same time, at the hardware level, the OS allocates and shares the processor. Now, just to prove my fact is trying to play the video in the background while this program is running, it should work (it might not be that smooth).

There is a technology inside the processor that prevents overheating from burning the processor. First it will speed up the FAN and then eventually it will shut off, the temperature will rise.



Yes, you are partially correct, FAN and cpuz take time to react. But if the temperature crosses the point, the processor will raise an interrupt and pull out all actions.

+1


source







All Articles