Does Java actually run threads in parallel

11,646

Solution 1

In short yes it does run on separate threads. You can test it by creating 100 threads and checking in your process explorer it will say 100 threads. Also you can do some computation in each thread and you will see your multicore processor go upto 100% usage.

Thread.currentThread gives you the current thread you are running from. When you start your program you are running on the "main" thread. As soon as you start a new thread

new Thread(myRunnable);

any code located in the myRunnable will run on the new thread while your current thread is is still on the main Thread.

If you check out the API http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html it gives allot more detailed description of the thread.

Solution 2

The actual threading mechanism can vary between CPU architectures. But the real problem is that you're misinterpreting the method name. Thread.currentThread() doesn't return the thread executing at the current moment in time; it returns the thread currently executing the method call, that is, itself.

Solution 3

Yes it does. Run any simple infinite loop on more than one threads and you'll see the cpu usage > 100% on a multi-core CPU.

Solution 4

Yes it does run threads concurrently .That is the very purpose of the multithreading concept .you may find the folowing discussion helpful : Current Thread Method java

Solution 5

Not a complete answer here, just adding to what others already have said:

The Java Language Specification does not require that threads run in parallel, but it allows them to do so. What actually happens in any given Java Virtual Machine depends on how that JVM is implemented.

Any practical JVM will create one "native" (i.e., operating system) thread for each Java thread, and let the operating system take care of scheduling, locking, waiting, notifying,...

Share:
11,646

Related videos on Youtube

Argho Chatterjee
Author by

Argho Chatterjee

Updated on July 04, 2022

Comments

  • Argho Chatterjee
    Argho Chatterjee almost 2 years

    I would like to understand that does java actually run multiple threads in parallel in a multi core CPU, or there is context switching between threads and only one thread is active and others are waiting for their turn to run.

    In other words, is there a possibility that 2 threads are running in parallel???

    Because my Thread.currentThread() does not give me a array of threads, but only one thread which is running. So what is the truth, does only one thread run at a time while others wait or multiple threads can run in parallel, if yes , then why my Thread.currentThread() method return only 1 thread object.

    Edit : .....

    I have created 2 classes to count numbers 1 class does it synchronously and the other one divides it into two halves and executes the two halves in 2 threads..(intel i5(4 CPUs), 8GB ram) the code is as follows :

    common class :

    class  Answer{
    long ans = 0L;}
    

    Multi Thread execution : public class Sheet2 {

    public static void main(String[] args) {
    
        final Answer ans1 = new Answer();
        final Answer ans2 = new Answer();
    
    
        Thread t1 = new Thread(new Runnable() {
    
            @Override
            public void run() {
                for(int i=0;i<=500000; i++) {
                    ans1.ans = ans1.ans + i;
                }
            }
        });
    
        Thread t2 = new Thread(new Runnable() {
    
            @Override
            public void run() {
                for(int i=500001;i<=1000000; i++) {
                    ans2.ans = ans2.ans + i;
                }
            }
        });
        long l1 = System.currentTimeMillis();
    
        try {
            t1.start();t2.start();
            t1.join();
            t2.join();
            long l2 = System.currentTimeMillis();
            System.out.println("ans :" + (ans1.ans + ans2.ans) +" in "+(l2-l1) +" milliseconds"); 
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    
    
    }
    

    }

    Single Thread execution : public class Sheet3 {

    public static void main(String[] args) {
    
        final Answer ans1 = new Answer();
    
        long l1 = System.currentTimeMillis();
    
        for(int i=0;i<=1000000; i++) {
            ans1.ans = ans1.ans + i;
        }
        long l2 = System.currentTimeMillis();
    
        System.out.println("ans :" + (ans1.ans ) +" in "+(l2-l1) +" milliseconds"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    
    }
    

    }

    My Single thread execution is faster than my multi threaded execution, I though the context switching was putting a overhead on the execution initially and hence the multithreaded execution output was slower, now I am having muti core CPU (4 CPU), but still single threaded execution is faster in this example..

    Can you please explain the scenario here... is it because my other processes are eating up the other cores and hence my threads are not running in parallel and performing time slicing on the CPU ???

    Kindly throw some light on this topic. Thanks in advance. Cheers.!!!

  • Argho Chatterjee
    Argho Chatterjee about 9 years
    if so then why does the api Thread.currentThread(), return only one thread, Is it that it only returns a reference of its own thread and not all the threads that are executing .???? I thought it does context switching and each threads take turns while executing the operation... and pass over the executing to other thread when it is performing some operations with external resources like DB or IO.
  • Argho Chatterjee
    Argho Chatterjee about 9 years
    thanks for correcting me here.. Yes indeed, so Should I assume that in multi cores, say 16 core machines, there can be 2-3 threads running in PARALLEL inside the same JVM and improve the response time . ?.
  • shmosel
    shmosel about 9 years
    @ArghoChatterjee That's a reasonable assumption.
  • user207421
    user207421 about 9 years
    @ArghoChatterjee It returns the thread that is executing the call to Thread.currentThread().
  • vz0
    vz0 about 9 years
    The Thread.currentThread() call returns a reference to the thread that is executing the function call. If there are many threads executing the same code, each call will return a different thread reference.