Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Thread overriding problem

 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package a.b.threads;
class A implements Runnable {
public void run() {System.out.println(Thread.currentThread().getName()); }
}

class B implements Runnable {
public void run() {
new A().run();
new Thread(new A(),"name_thread2").run();
new Thread(new A(),"name_thread3").start();
}
}

public class C
{
public static void main(String[] args)
{
new Thread(new B(),"name_thread1").start();
}
}

============================
Output -
name_thread1
name_thread1
name_thread3

============================
Hi

I am not able to understand the working of above mentioned program? means how compiler is reaching to output as
"name_thread1
name_thread1
name_thread3
" ?

Also what is the difference between "new Thread(new A(),"name_thread2").run(); " and "new Thread(new A(),"name_thread3").start();" statement ? how JVM/compiler is executing these statements ?

Please reply

Thanks
Abhay
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the [code] tags if you're posting code

The difference between "new Thread(new A(),"name_thread2").run(); " and "new Thread(new A(),"name_thread3").start();":
the start() method creates a new Thread and executes the run method in that thread. If you invoke the run() method directly, the code in run will execute in the current thread. This also expains why your code prints two lines with the same thread name.
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Start with the main method...
you create a new thread with name "name_thread1" and called start() on that. Now the run() of Class B will be called as a result of calling start().
When the control goes in run() of class B it encounters the statement::
new A().run();
Here you are not creating any new thread, but simply calling the run() method of class A. Now the point to note is that when you explicitly call a run() method like above, you are not creating any new thread but simply calling a function of that class. Hence in the code:::
public void run() {System.out.println(Thread.currentThread().getName()); }
Thread.currentThread().getName()----------> gives "name_thread1" as new A().run() was called by this thread i.e. "new_thread1". Hence you twice get "new_thread1".

Now when the controls goes to this line::
new Thread(new A(),"name_thread2").run();
Here again you are not calling the start() but explicitly calling the run() of Thread Class. And since the run() of Thread class does nothing hence nothing is printed on the screen.

And finally when the the control goes to:::
new Thread(new A(),"name_thread3").start();

a new thread is created.. start() is called whic in turns calls the run() and hence "new_thread3" is printed.

Hopefully got what I wanted to say.
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Thanks for your reply. It was really helpful

Only thing is

we are getting "name_thread1" from " new Thread(new A(),"name_thread2").run();" statement

When i ran below mentioned code , i get "name_thread1" output


package a.b.threads;
class A implements Runnable {
public void run() {System.out.println(Thread.currentThread().getName()); }
}

class B implements Runnable {
public void run() {
new Thread(new A(),"name_thread2").run();
}
}

public class Test2
{
public static void main(String[] args)
{
new Thread(new B(),"name_thread1").start();
}
}

Output -
name_thread1

Thanks
Abhay Agarwal
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhay Agarwal ,just understand that calling run() method does not start a new thread.
To start a new thread you MUST use start() method.

In your case , when control reaches new Thread(new A(),"name_thread2").run(); , run() is called , like calling any other mehod.

 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!