Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many java threads can be created for a dual core processor

So the STB is dual core. I thought we can only create 2 proper threads.

In every keyreleased()

I am creating a new thread

Runnable runnable = new Runnable() 
{
    int i = j;

    public void run() 
    {
        while (true) 
        {
            System.out.println("This thread is running always number is " + i);
        }
    }
};

Thread th = new Thread(runnable);
            th.setPriority(Thread.MAX_PRIORITY);
            th.start();

j++;
//...
}

But even after creating 20 more threads, box doesn't have any issues.

Is it because JVM realized that the run block is empty and it optimized the code ? Or is the JVM implementation for while(true) is different ?

Note: i have tried putting Thread.sleep(1000) as well but no problems

like image 692
Jeril Kuruvila Avatar asked Jul 19 '26 04:07

Jeril Kuruvila


1 Answers

You can run 20 threads even on a single-core machine. What happens is called time slicing.

http://en.wikipedia.org/wiki/Time_slice#Time_slice

It is a way for the processor to simulate multiple processors executing multiple tasks as once.

like image 56
peter.petrov Avatar answered Jul 21 '26 17:07

peter.petrov