AUK
AUK

Reputation: 31

Why OS struck on creating infinite java threads

I was just curious and written following code. On running code, console showed threads created. I noticed there were around 32k threads created. My question is, Why My windows 10 OS hanged and also could not stop forcefully with task manager. Since my machine has i7 processor, why OS didn't manage to run on other processes? I believe threads created by java program must have been in one of the processes.

I am a beginner to java. Kindly let me know what happens internally in above case?


package infiniteThreads;

public class Main {

    public static void main(String[] args) {
        while(true){
            Thread t = new Thread(new Work());
            t.start();
        }

    }

}

package infiniteThreads;

public class Work implements Runnable{

    @Override
    public void run() {
        while(true){
            System.out.println("inside " + Thread.currentThread().getName());
        }

    }

}

Upvotes: 0

Views: 84

Answers (1)

y ramesh rao
y ramesh rao

Reputation: 3004

As far as java is concerned, it would be consuming all the processors and all CPU threads available on your system. But the limitation that you are hitting with your experiment here is an OS Level limitation which is around Handles could read more about the it here.

Maybe you could try fiddling things around with your system setup to try to get some more insights with your experiment could try this link.

Upvotes: 2

Related Questions