aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes start and run method 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 "start and run method" Watch "start and run method" New topic
Author

start and run method

samuel ebenezer
Greenhorn

Joined: Aug 03, 2007
Posts: 16


This code wont give any output. But If I swap the start and run names in code, it prints output from 0 to 4. How?
kaushik vira
Ranch Hand

Joined: Feb 01, 2007
Posts: 102
What you did is not right coding style. and not following Thread Life cycle.




When you create Thread Object. you should overrid run() method which is actual task will done by Thread.

start() default called the run method. so The right why to write a code is.

public class Test extends Thread
{
public static void main(String args[])
{
Test t = new Test();
t.start();
}
public void run()
{
for(int i=0;i<5;i++)
{
System.out.println("value of i:"+i);
}
}
}

- Don`t override start() until you don`t have any special purpose.
- Always override run() cause it`s the actual task done by Thread.
[ July 21, 2008: Message edited by: kaushik vira ]

kaushik Vira
-------------------------------------
SCJP, Preparing SCWCD..
samuel ebenezer
Greenhorn

Joined: Aug 03, 2007
Posts: 16
Thanks kaushik for your explanation, but what is exactly happening in my code? Is Thread referring to overloaded start method or its own start method? Still i didnt get.. Please elaborate..
samuel ebenezer
Greenhorn

Joined: Aug 03, 2007
Posts: 16
sorry it is not overloaded, what i want to say is overrided..
kaushik vira
Ranch Hand

Joined: Feb 01, 2007
Posts: 102
yes, you Override start() method. Default implementation for start() method is

public synchronized void start() {
if (started)
throw new IllegalThreadStateException();
started = true;
group.add(this);
start0();
}

private native void start0();

Which actually a native call which start Thread execution parallel to main thread.

But in your case, Main Thread is only doing all this things. try to print Thread Id with println() you will get more clear idea about parallel execution of Thread.
[ July 21, 2008: Message edited by: kaushik vira ]
kaushik vira
Ranch Hand

Joined: Feb 01, 2007
Posts: 102
Try to Run this 2 Example:

-------------------------------------------------------
public class Test2 extends Thread
{
public static void main(String args[])
{
Thread.currentThread().setName("Main Thread.");
Test2 t = new Test2();
t.setName("Child Thread.");
t.start();
}
public void start()
{
for(int i=0;i<5;i++)
{
System.out.println("value of i:"+i + " By " + Thread.currentThread().getName());
}
}
}
-----------------------------------------------------------------------
public class Test extends Thread
{
public static void main(String args[])
{
Thread.currentThread().setName("Main Thread.");
Test t = new Test();
t.setName("Child Thread.");
t.start();
}
public void run()
{
for(int i=0;i<5;i++)
{
System.out.println("value of i:"+i + " By " + Thread.currentThread().getName());
}
}
}
------------------------------------------------------------------------
kaushik vira
Ranch Hand

Joined: Feb 01, 2007
Posts: 102
and now if you clear with both above. Try this.

public class Test2 extends Thread
{
public static void main(String args[])
{
Thread.currentThread().setName("Main Thread.");
Test2 t = new Test2();
t.setName("Child Thread.");
t.start();
}
public void start()
{
super.start();
for(int i=0;i<5;i++)
{
System.out.println("value of i:"+i + " By " + Thread.currentThread().getName());
}

}
public void run()
{
for(int i=0;i<5;i++)
{
System.out.println("value of i:"+i + " By " + Thread.currentThread().getName());
}
}
}
samuel ebenezer
Greenhorn

Joined: Aug 03, 2007
Posts: 16
In the first Test2 class, Main thread is executing the thread we created because we overrided start().. and in the second Test class, Child Thread is executing because we didnt overrided start() and executes run().. This is clear. If you take the next example, we overrided both start() and run().. here the order of execution is bit confusing..

The output for the third example is like this..

Value of i:0 by Main Thread.
Value of i:1 by Main Thread.
Value of i:0 by ChildThread
Value of i:1 by ChildThread
Value of i:2 by ChildThread
Value of i:3 by ChildThread
Value of i:4 by ChildThread
Value of i:2 by Main Thread.
Value of i:3 by Main Thread.
Value of i:4 by Main Thread.

Initially overrided start() method executes.. after two iterations, run() executes. After completion of run() again overrided start() comes in to loop.. Can you please explain me this.. Please correct me if am wrong any where
Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 118
@samuel
What happens in the code that you posted initially ...

> you don' get compile error cause you are extending class Thread & it is legal to overide its non-private methods... run() & start() are public in class Thread
>what does start() do? ... creates another thread & calls run() thats it!
you are overriding it! & so now it won't call run()!
>when you are saying t.run(); you are calling the default run() method declared to do NOTHING in class Thread! So no output!! & no new thread creation.
>when you swap the methods in your code it works fine ... cause you over ride the default run(0 method & give it to do something for you. You are calling start() by t.start() .... the default version invokes run() method.


SCJP 6 83%
Working on SCWCD/OCPJWCD
kaushik vira
Ranch Hand

Joined: Feb 01, 2007
Posts: 102
third example is also very clear.

We are calling start() method, right. and start() internally calling run() in default case..

But here i override start(). which doing 2 task

1 :- super.start();
2 for(int i=0;i<5;i++)System.out.println("value of i:"+i + " By " + Thread.currentThread().getName());


Main Thread will executed start() (will create Thread and call run. + execute for loop )
child Thread will executed run() (execute run() by chilled Thread)
samuel ebenezer
Greenhorn

Joined: Aug 03, 2007
Posts: 16
Thanks guys for your wonderful explanation. It is very clear now..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: start and run method
 
Similar Threads
Q from Whizlabs
Java thread question...
Thread Execution
What will be the output....Please explain
Thread