vmr
vmr

Reputation: 1935

Programatically check if two threads belong to the same process in Java

How do I check if two threads belong to same or different processes programatically? This is the piece of code I have written:

public class MyThread {

    public static void main(String[] args) {
        TestThread1 obj1 = new TestThread1();
        TestThread2 obj2 = new TestThread2();
        System.out.println("Current thread:" + Thread.currentThread().getName());
        Thread t1 = new Thread(obj1);
        t1.start();
        Thread t2 = new Thread(obj2);
        t2.start();


    }
}

class TestThread1 implements Runnable {
    @Override
    public void run () {
        for(int i=0; i<1000 ;i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
            System.out.println("Current thread:" + Thread.currentThread().getName());
        }
    }
}

class TestThread2 implements Runnable {
    @Override
    public void run () {
        for(int i=0; i<1000 ;i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
            System.out.println("Current thread:" + Thread.currentThread().getName());
        }
    }
}

Here what I understand is thread t1 being created as part of process TestThread1 and thread t2 being created as part of TestThread2 process. But how do i check this programatically?

Upvotes: 2

Views: 778

Answers (3)

Stephen C
Stephen C

Reputation: 718916

There is something wrong with your understanding or your use of terminology.

Here what I understand is thread t1 being created as part of process TestThread1 and thread t2 being created as part of TestThread2 process.

First of all, some terminology:

  • TestThread1 and TestThread2 are classes not processes.

  • The values in obj1 and obj2 are not processes either. They are instances of the TestThread1 and TestThread2 classes respectively.

If I interpret your question correctly, you are actually asking if there is a way to find out if t1 and t2 share a single Runnable instance. Unfortunately, there isn't a way to do that in pure Java. A Thread object's Runnable is a private field and there is no public getter for retrieving it1.

On the other hand, if you are really asking about processes ...

When you run the application, there will be only one process, and both threads will belong to it.


1 - It is possible to use nasty reflection to retrieve the private field's value, but it is a bad idea. You should look for a way to do whatever you are trying to do that doesn't entail this test ...

Upvotes: 2

Dragan Bozanovic
Dragan Bozanovic

Reputation: 23552

Maybe there is an easier way, but you could call getStackTrace() on a thread and then inspect it (search for the run method's frame). However, this will work only for live threads.

PS You use wrong terminology here, what you refer to is not a process, it is just a class that defines the run method which is executed by the thread.

Upvotes: 0

AdamSkywalker
AdamSkywalker

Reputation: 11609

You mixed the concept of threads and processes.

thread t1 being created as part of process TestThread1 and thread t2 being created as part of TestThread2

Your TestThread1 and TestThread2 are just runnables, that hold information what action should be done by thread. Your t1 and t2 are actual threads, they run in the same process, because you started them in one application. All threads in the application run in same java process, so you can't have a situation where you have two threads referecens and they belong to different processes.

If you start another java application, it will run in a different process, but you won't be able to compare two threads from different processes in a single context.

Upvotes: 2

Related Questions