Win a copy of Spring Boot in Practice this week in the Spring 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Problem in thread

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends I cannot determine why the output is such.


public class Test {
public static void main(String[] args)
{
new MyThread().start();
new MyThread(new MyRunnable()).start();
}

}
class MyThread extends Thread
{
MyThread(){}
MyThread(Runnable r){super(r);}
public void run()
{
System.out.println("Inside Thread");
}
}
class MyRunnable implements Runnable
{
public void run()
{
System.out.println("Inside runnable");
}
}


The output of the code is

Inside Thread
Inside Thread
 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check This similar problem
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You never run the MyRunnable thread.
To start a thread which is coded using implementing the Runnable Interface, you need to pass the runnable object to the Thread class.
Now in your code, you never pass the object to Thread class but instead pass it to MyThread class which extends the Thread class.
Since both the calls call MyThread's run method, so Inside Thread is printed twice!

Try this code:



Can you observe the difference now?
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The way you start a thread depends on the way you have defined it. If you have extended Thread class, then starting it is very simple. Just Create an instance and call Start method on that instance.

When you implement Runnable interface, you need to do three things
1. Create an instance of Class that implements Runnable.
2. Create a Thread instance and pass it the instance that is created in Step 1

3. Call the start method on the Thread instance.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic