• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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: 9691
42
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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic