Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads

 
Lalitha Vydyula
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
--------------------------------------------------------------------------------
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Um ... explain what? Does the code not do what you were expecting it to do? Then you should describe what you were expecting.
 
Lalitha Vydyula
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Lalitha Vydyula
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marc and Marilyn for your replies.
 
Marilyn de Queiroz
Sheriff
Posts: 9064
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please do not post the same question in multiple forums.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic