Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing two threads with the same instance of a runnable

Is it bad programming to initialize two threads with the same instance of a runnable? What difference would it make to initialize with separate instances of a runnable, and does sharing memory locations at all for the same instance of a runnable have anything to do with 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());
        }
    }
}
like image 381
Sam Adamsh Avatar asked Mar 05 '12 06:03

Sam Adamsh


People also ask

Can you make multiple thread to execute same instructions?

Yes, A program can run two threads at the same time. it is called Multi threading.

What happens when two threads have same priority?

If two threads of the same priority are waiting for the CPU, the scheduler arbitrarily chooses one of them to run. The chosen thread runs until one of the following conditions is true: A higher priority thread becomes runnable. It yields, or its run method exits.

Can two threads have same priority?

It is possible to have same priority to threads. So CPU can decide which thread to run by using some algorithms.

Can a runnable be run multiple times?

Since Daemon class is not threadsafe, then it is dangerous to run same instance (of type Runnable) from multiple threads. It is always dangerous to run methods of the same instance from multiple thread, unless the access to the object is protected with a monitor (synchronized keyword).


3 Answers

It's absolutely fine to do it so long as the code you're running is designed to support that. Not only will it save some memory by having a single instance instead of multiple instances, but if those threads are trying to communicate via shared data, then it may be absolutely required!

Admittedly communicating via shared state is where threading often gets tricky, so this needs to be done carefully, but from the point of view of the threading system itself, there's absolutely no problem in having two threads call the run method of a single Runnable instance.

like image 171
Jon Skeet Avatar answered Sep 21 '22 20:09

Jon Skeet


Since H doesn't have any instance state, using multiple instances won't matter. You need to take care when the Runnable instances start storing state.

public class Main implements Runnable {     volatile int i;         public void run() {         for (i = 0; i < 100; i++) {             System.out.println(i);         }     }      public static void main(String[] args) {         Main a = new Main();         Thread t1 = new Thread(a);         Thread t2 = new Thread(a);         t1.start();         t2.start();     }         } 

What gets printed? When you do need to share state between threads, it's a good idea to use the classes in java.util.concurrent. They were written primarily by an expert in multithreading (Doug Lea, author of Concurrent Programming in Java) and tested by many people. Save yourself some heartache. :)

like image 42
David Harkness Avatar answered Sep 25 '22 20:09

David Harkness


Is it bad programming to initialize two threads with the same instance of a runnable?

Not specifically. However, if the Runnable instance has instance fields, then you'll need to make sure that all access to the fields by the thread is properly synchronized, and this will make the code more complicated.

What difference would it make to initialize with separate instances of a runnable, and does sharing memory locations at all for the same instance of a runnable have anything to do with performance?

The memory saved by sharing a Runnable instance between multiple threads is insignificant ... unless the Runnable holds a significant amount of instance data. (And if it does, the chances are that this will make the instance non-shareable.)


Your H class is an example where sharing instances is safe, but pointless since the memory saving is insignificant. (A Runnable object with no instance fields occupies roughly 8 to 16 bytes, depending on the platform.)

like image 33
Stephen C Avatar answered Sep 23 '22 20:09

Stephen C