aspose file tools*
The moose likes Threads and Synchronization and the fly likes Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads" Watch "Threads" New topic
Author

Threads

Lalitha Vydyula
Ranch Hand

Joined: Jul 20, 2005
Posts: 69
--------------------------------------------------------------------------------
Hi all,
Can anyone explain the flow of the below code. I'm really confused. Your help will be appreciated.

class NewThread implements Runnable
{
Thread t;
NewThread()
{
t = new Thread(this, "Demo Thread");
System.out.println("Child Thread: " + t);
t.start();
}

public void run()
{
try
{
for(int i=5;i>0;i--)
{
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
}
catch(InterruptedException e)
{
System.out.println("Child interrupyed.");
}
System.out.println("Exiting child thread");
}
}

class ThreadDemo1
{
public static void main(String s[])
{
new NewThread();
try
{
for(int i=5;i>0;i--)
{
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println("Child interrupted.");
}
System.out.println("Main thread exiting");
}
}
------------------------------
Here is the output
------------------------------
Child Thread: Thread[Demo Thread,5,main]
Main Thread: 5
Child Thread: 5
Child Thread: 4
Main Thread: 4
Child Thread: 3
Child Thread: 2
Main Thread: 3
Child Thread: 1
Exiting child thread
Main Thread: 2
Main Thread: 1
Main thread exiting

Thanks,
-Lalitha
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41809
    
  62
Um ... explain what? Does the code not do what you were expecting it to do? Then you should describe what you were expecting.


Ping & DNS - my free Android networking tools app
Lalitha Vydyula
Ranch Hand

Joined: Jul 20, 2005
Posts: 69
Hi Ulf,
Actually I got little confused at line t.start(). I thought as soon as we call the start() method the run() will get executed. But now its clear that calling start() would actually put the thread in run mode and it depends on the JVM when to execute run().

Please correct me if I'm wrong.

-Lalitha
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Lalitha Vydyula:
...now its clear that calling start() would actually put the thread in run mode and it depends on the JVM when to execute run()...

Well, calling start makes the thread eligible to run, and it's up to the thread scheduler to call the run method.

See responses to your duplicate post:
http://www.coderanch.com/t/400429/java/java/Threads
[ August 03, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Lalitha Vydyula
Ranch Hand

Joined: Jul 20, 2005
Posts: 69
Thanks Marc and Marilyn for your replies.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Please do not post the same question in multiple forums.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads