0

Context

Let's say I have a simple Java multi threading program with

class Runner1 implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("Runner1: " + i);
        }
    }
}

class Runner2 implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("Runner2: " + i);
        }
    }
}

public class App {
    public static void main(String[] args) {
        Thread t1 = new Thread(new Runner1());
        Thread t2 = new Thread(new Runner2());

        t1.start();
        t2.start();
    }
}

The program is running on a multiple core laptop e.g. 4 cores. My understanding from related post is that multithreading can be executed on a single core.

Question

I was wondering about the behavior of the JAVA 2 threads execution on the cpu.

Will the 2 threads be executed by a single cpu core or will they be allocated to different cpu core for execution? Is there a mechanism or default scenario to decide the allocation?

Thanks!

keepgoing
  • 13
  • 3
  • thread - is a unit of execution. and two threads `CANT` run on the same cpu core at the same time. – Alexander Ivanchenko Jan 31 '22 at 00:07
  • ...But two threads _can_ run _concurrently_ on a single-CPU machine. They just can't both be running at any single instant in time. On a single-CPU machine, the operating system periodically pauses whichever thread currently is running, and it allows some other thread to run. _Which_ other thread is a dark mystery known only to the high priests of the OS. – Solomon Slow Jan 31 '22 at 00:17
  • 1
    P.S., Something similar happens when _hundreds_ of threads want to run concurrently on a machine with four or eight or 16 CPUs. – Solomon Slow Jan 31 '22 at 00:19
  • 2
    @AlexanderIvanchenko Hi, I am aware that they cannot run at the same time. The question here is regarding the allocation. I wonder how is it decides to allocate to a single or multiple core – keepgoing Jan 31 '22 at 00:25

3 Answers3

2

Each thread has an execution stack that tracks its state. It does not matter whether the two threads run on the same CPU or different CPU. In fact what usually happens is that a thread runs a little bit at a time, and the same CPU can be switch back and forth, running one thread for a while, and running the other for a while.

When the threads are allocated to different CPUs, then the possibility of conflicting updates to shared objects is greater, because the threads could be running at the same time causing fine-grained interweaving of updates to memory. If you design your threads to protect against concurrency problems, then it really does not matter much whether they run on the same CPU or different CPU. It does not matter if the thread is run sometimes on one CPU, and later by a different CPU. The thread holds all its own state and does not matter which CPU runs it.

It seems that Java delegates to the operating system the decision on where to run a particular thread.

AgilePro
  • 5,588
  • 4
  • 33
  • 56
0

It can be run on a single-core, but care must be taken when one thread is doing read and the other one does write operation. Internal workings are taken care of by the OS (scheduler).

Sivaram
  • 1
  • 1
0

There is not much to add to the answer of AgilePro.

If you want to control which CPU runs which threads, you need to pin down the thread to a CPU. You could use Thread Affinity library of Peter Lawrey when using Java.

On Linux, every thread has a bitmap with a single bit per CPU the thread is allowed to run on. By default, all bits are set, meaning the thread can run on any CPU. And thread can even migrate from one CPU to another. But using certain system calls, one can pin down a thread to a subset (even a single) of CPUs.

pveentjer
  • 10,545
  • 3
  • 23
  • 40