1
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.

14
  • 2
    What comment part? Also, if you don't tell the CPU to sleep, it won't sleep. Commented Jul 13, 2017 at 11:42
  • 4
    100 Threads doing an infinite loop... Commented Jul 13, 2017 at 11:45
  • 1
    "the program i'm writing does not need the cpu to sleep" What is the problem then? Commented Jul 13, 2017 at 11:47
  • 1
    in other words, starting 100 Threads, each doing infinite loops of something as fast as possible => the CPU is fully occupied unless having more than about 100 cores. Commented Jul 13, 2017 at 11:49
  • 1
    @SteveSmith thank you so much for your information Commented Jul 13, 2017 at 12:26

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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?

1 Comment

Even that wont help. There would be at most 4 cores in CPU qnd 100 threads running in parallel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.