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());
}
}
}
Yes, A program can run two threads at the same time. it is called Multi threading.
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.
It is possible to have same priority to threads. So CPU can decide which thread to run by using some algorithms.
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).
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.
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. :)
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With