GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes can anyone explain this program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "can anyone explain this program" Watch "can anyone explain this program" New topic
Author

can anyone explain this program

Chandana Garlapati
Ranch Hand

Joined: Jul 06, 2009
Posts: 97
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

Joined: Feb 23, 2009
Posts: 1183
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


JDBCSupport - An easy to use, light-weight JDBC framework -
Chandana Garlapati
Ranch Hand

Joined: Jul 06, 2009
Posts: 97
Thanks Sebastian Janisch.
I also have another doubt what should be modified in code if i want a output "Jaguar".
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
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

Joined: Jul 06, 2009
Posts: 97
ya its ridiculous!! anyways.. the output is lynx is 3 times in above code.

once again Thanks Sebastian Janisch...
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
true 4 times not 3 ...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

ya its ridiculous!!


BTW, what's ridiculous?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Chandana Garlapati
Ranch Hand

Joined: Jul 06, 2009
Posts: 97
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

Joined: Jul 06, 2009
Posts: 97
i am saying about my question dont think wrong Henr Wong...
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
gee.. my mistake ... it's 3 outputs not 4, i typed wrong
Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83
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!!!
 
GeeCON Prague 2014
 
subject: can anyone explain this program