• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can anyone explain this program

 
Chandana Garlapati
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am new to java. The following program is from iRix Technologies...How the code is executed??

class Foo implements Runnable {
public void run( ) {
for (int x = 1; x < 4; x++) {
System.out.print( "Lynx " );
}
}

public static void main(String[ ] args) {

Foo f = new Foo( );
Thread t = new Thread(f);
t.setName( "Jaguar " );
t.start( );
}
}
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey,

if you are totally new to java you should not start with the concept of threading ...

what the code does is declaring the class Foo as type Runnable.

The java.lang.Thread class implemnts Runnable, therefore Thread is Runnable ..

the code in the main method creates a new Foo object and hands it to a Thread object ..

once you invoke start on the Thread instance, the run() method in the Foo object will be executed ...
run() is implemented from the Runnable interface ...

The deal about Threads is that each Threads runs on it's own stack, having multiple CPU processors, multiple Threads can run simultanouesly ...
even with only 1 cpu, it appears as though several threads are executing simultanoeusly ...

hope this helps
 
Chandana Garlapati
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sebastian Janisch.
I also have another doubt what should be modified in code if i want a output "Jaguar".
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
uhm, what the code does so far is outputting Lync 4 times ...

jaguar is the name of the thread ... if you using an environment like eclipse in debugging mode, you will see each thread and it's name ... it's mainly for debugging purposes ...

to output jaguar, you need to change the code in the run method to output jaguar instead of lync
 
Chandana Garlapati
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya its ridiculous!! anyways.. the output is lynx is 3 times in above code.

once again Thanks Sebastian Janisch...
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
true 4 times not 3 ...
 
Henry Wong
author
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya its ridiculous!!


BTW, what's ridiculous?

Henry
 
Chandana Garlapati
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How it is 4 times?? x=1 and x<4 right so it should be 3 times..

and i executed it prints lynx just 3 times..
 
Chandana Garlapati
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am saying about my question dont think wrong Henr Wong...
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gee.. my mistake ... it's 3 outputs not 4, i typed wrong
 
Mo Jay
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chandana Garlapati , to output the thread name(Jaguar) instead of "Lynx" you just need to remove Lynx from System.out....and replace it with the method that gets the thread name: Thread.currentThread().getName()

Cheers!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic