keepgoing
keepgoing

Reputation: 13

Java multi-threading execution on CPU core

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!

Upvotes: 0

Views: 1949

Answers (3)

pveentjer
pveentjer

Reputation: 11402

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.

Upvotes: 0

Sivaram
Sivaram

Reputation: 1

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

Upvotes: 0

AgilePro
AgilePro

Reputation: 5616

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.

Upvotes: 2

Related Questions