Sarasr
Sarasr

Reputation: 43

cpu usage multithreading java

public class Main {
    public static void main(String[] args) {
        LinkedList<Th> myThreads = new LinkedList<>();

        for (int i = 0; i < 100; i++) {
            myThreads.add(new Th());
            myThreads.get(i).start();
        }
    }
}

public class Th extends Thread {

    int myInt;

    @Override
    public void run() {
        while(true) {
           doSomething();
        }
    }

    private void doSomething() {
        switch (myInt) {
            case 0: {
               // System.out.println("comment part");
                break;
            }
            case 2:{
                System.out.println("2");
                break;
            }
        }
    }
}

Hi, I have trouble with a simple multithreading in java. when I run this program, cpu usage is suddenly 100% and my computer crashes and I can't stop the program. Here you can see the code. when I un-comment the comment part, everything is ok! but I need to fix this without printing anything in the console. PS. I already know that case 2 codeblock is never compiled.

Upvotes: 1

Views: 452

Answers (2)

JaDogg
JaDogg

Reputation: 1313

Add a sleep. Simply call sleep method where the print is. Your CPU should sleep for some milliseconds to avoid consuming it for 100%. In your case the console-io acts as a sleep.

Try reducing the threads and use a round robin method to do the calculations.

Round-robin (RR) is one of the algorithms employed by process and network schedulers in computing. As the term is generally used, time slices (also known as time quanta) are assigned to each process in equal portions and in circular order, handling all processes without priority (also known as cyclic executive). See https://en.wikipedia.org/wiki/Round-robin_scheduling

Suggested Modification:

private void doSomething() {
    switch (myInt) {
        case 0: {
            try {
                sleep(100);
            } catch (InterruptedException e) {
                interrupt();
            }
            break;
        }
        case 2:{
            System.out.println("2");
            break;
        }
    }
}

You should call the interrupt method to re-interrupt the thread. Refer: Why invoke Thread.currentThread.interrupt() when catch any InterruptException?

Upvotes: 1

Davide Spataro
Davide Spataro

Reputation: 7482

This code has nothing wrong in its own. It's your machine that can't cope with the amount of work you required it to do.

Your code spawn 100 threads which will print something to the standard output (costly operation, since you have to ask the OS to do that) as fast as they can. This is a lot of work even for a modern machine.

Try to give up the CPU voluntarily for a while adding a sleep after you have printed out your message.

switch (myInt) {
            case 0: {
                System.out.println("message");
                Thread.sleep(50);
                break;
            }

Plus, there is nothing wrong in having 100%usage of the CPU. You are simply using the whole computational power available. It is something that is good, especially in scientific computation.

Upvotes: 4

Related Questions