The first way is this:
public class Demo {
public static void main (String args[]) {
Apple a1 = new Apple();
Apple a2 = new Apple();
Thread t1 = new Thread(a1, "First Thread");
Thread t2 = new Thread(a2, "Second Thread");
t1.start();
t2.start();
}
}
The second way is this:
public class Demo {
public static void main (String args[]) {
Apple a = new Apple();
Thread t1 = new Thread(a, "First Thread");
Thread t2 = new Thread(a, "Second Thread");
t1.start();
t2.start();
}
}
The void run()
method is in Apple class and I did not paste it here.
Seems like in the first situation I created 2 Apple class objects and pass them respectively to t1
and t2
. While in the 2nd situation I pass the same Apple class object to t1
and t2
. What's the real difference in terms of multi-threading? Could you suggest me which way is correct and recommended? Thank you!