• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on therad k&b 739 q.14

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ranchers,

class Mytherad extends Thread
{
Mythread()
{
System.out.print("Mythread");
}

public void run()
{
Sytem.out.print("bar");
}
public void run(String s)
{
System .out.print("hi");
}
}

class Test
{
public static void main(string a[])
{
Thread s= new Thread()
{
public void run()
{
System.out.print("foo");
}
};
t.start();
}
}
ans--Mythread foo but the ans should be Mythread bar.
You can also see the some thing different in this code

Can someone expalin


 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There seems to be something missing from this code. I'm not sure if there are typos - the main method refers to a variable named t, but only a variable s is declared there. Also s is of type Thread not Mythread.

If s is of type Thread then the output will be foo.

If we declare s as follows:


then the output will be
Mythreadfoo

as we have over-ridden the run method to print out foo.
 
Ranch Hand
Posts: 376
Scala Monad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which thread are you starting with "t.start();" ?
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry,
Mymistake, it is s.start();
Why it is printing foo.
It should print Mythreadbar.
Can you explain me
 
Ian Edwards
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read my explanation above - it's because you have over-ridden the run() method.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic