multithreading - Programatically check if two threads belong to the same process in Java -
how check if 2 threads belong same or different processes programatically? piece of code 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 understand thread t1 being created part of process testthread1 , thread t2 being created part of testthread2 process. how check programatically?
you mixed concept of threads , processes.
thread t1 being created part of process testthread1 , thread t2 being created part of testthread2
your testthread1
, testthread2
runnables, hold information action should done thread. t1
, t2
actual threads, run in same process, because started them in 1 application. threads in application run in same java process, can't have situation have 2 threads referecens , belong different processes.
if start java application, run in different process, won't able compare 2 threads different processes in single context.
Comments
Post a Comment