java - Initializing two threads with the same instance of a runnable -


is bad programming initialize 2 threads same instance of runnable? difference make initialize separate instances of runnable, , sharing memory locations @ same instance of runnable have performance?

public static void main(string[] args)throws exception {    h h = new h();    h h2 = new h();    thread j = new thread(h);    j.setname("11");     thread jj = new thread(h);//instead of new h()    jj.setname("22");    j.start();    jj.start(); }  class h implements runnable {     public void run() {         while(true) {            system.out.println(thread.currentthread().getname());         }     } } 

it's absolutely fine long code you're running designed support that. not save memory having single instance instead of multiple instances, if threads trying communicate via shared data, may absolutely required!

admittedly communicating via shared state threading gets tricky, needs done carefully, point of view of threading system itself, there's absolutely no problem in having 2 threads call run method of single runnable instance.


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -