Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Threads

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

This is a program from Vals exam can someone explain path of execution of this program.
Thanks
[Edited by Val - Added code tags]
[ July 02, 2002: Message edited by: Valentin Crettaz ]
 
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The above code canbe interpreted as follows..
HTH
murthy

[ July 02, 2002: Message edited by: Murthy Ram ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good intention Murthy, but your code created two objects Question11 which is not really elegant.
I would rewrite your code like this:

Note that when we create the thread t, we provide a reference to this Question11 object.
 
Deepali Pate
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But in my original code where is the implements Runnable happening???
 
Deepali Pate
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the implements is not mentined then can we still create and object of Runnable??
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Runnable is an interface and can't be instancitated. however, the in-line implementation of the runnable interface can be done as shown in the original code, as an annonymous inner class and doesn't require the implements clause.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the your original code..
you are creating the anonymous inner class(Runnable implicitly implementing Runnable interface) in the thread constructor. providing the implementation for run and then scheduling the thread using start().
 
Deepali Pate
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thread t = new Thread(new Runnable(){public void run(){for(int j=0;j<=i;j++){System.out.print(" "+j);}}});
This one statement is a way of creating an annonymous object of Runnable interface ???
Is my understanding right?
I still wonder how Runnable gets implicitly implemented is that true with any interface.
Thnx for ur replies
B
 
Amir Ghahrai
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thread t = new Thread(new Runnable(){ public void run(){ for(int j=0;j<=i;j++){ System.out.print(" "+j); } } });
This one statement is a way of creating an annonymous object of Runnable interface ???

That's correct. although the syntaxt may be a little bit strange, you are actually implementing the Runnable interface. You can implement any interface like that as an anonymous inner class, the same you do with event listener interfaces.
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic