aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

vidhya subramaniam
Ranch Hand

Joined: Jul 14, 2001
Posts: 91
i know this is a very basi doubt but just wanted to clarify.
We know that for making a thread run, you call the start method and then invoke the run() method. But can a thread start running without callin the start() method.
For example the foll code compiles cleanly.
public class NiceThreads implements Runnable
{
public void run()
{
while(true)
{
}
}

public static void main(String args[])
{
NiceThreads nt1 = new NiceThreads();
NiceThreads nt2 = new NiceThreads();
NiceThreads nt3 = new NiceThreads();
nt1.run();
nt2.run();
nt3.run();
}
}
I thought it wont compile as you havent set the start() method.
Please clarify.
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Nice question can anybody explain...


Spritle Software Blogs
Namrata Shetty
Ranch Hand

Joined: Jul 18, 2001
Posts: 30
what u have done is simply created an instance of the class and called the run method of it. The right way to create a thread is:

NiceThreads nt1=new NiceThreads();
Thread t1=new Thread(nt1);
t1.start();
Guoqiao Sun
Ranch Hand

Joined: Jul 18, 2001
Posts: 317
By simply call the run() method, you didn't start the Thread. That call has no difference with a normal method call.
Guoqiao


Guoqiao Sun<br />SCJP2 SCWCD2<br />Creator of <a href="http://www.jiris.com/" target="_blank" rel="nofollow">www.jiris.com</a>, Java resource, mock exam, forum
Nisheeth Kaushal
Ranch Hand

Joined: Jul 20, 2001
Posts: 87
Hi
What u have done is simply extending a class and then creating its instnace, u are not actually creating a thread if u are extending a thread u must call its start method as well as u must create an instance of that thread and then call start method on that particular thread hope it help.
Although i am also new in the topic of thread please send me some ideas on which basically the exam questions are based.
Thanx.
Nisheeth.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Just to throw in my 2 cents.
If you call the run method of a Thread object directly it is the same as calling any other method of any other object - the method will run in the current thread of execution. Calling the start method of a Thread object registers it with the scheduler and does some other behind the scenes work to make it work as a seperate execution process.
Notice the difference in the capitalization a Thread is an object of type Thread or that extends type Thread, while a thread is seperate execution process.
I've seen several questions of this type on the mocks I've taken so it might be a type you can expect to see on the scjp.
hope that helps
Dave


Dave
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi,
Dave has given a good explanation. I just modified your code, to see what happens if you start the thread using start() and what happens if you directly run() the thread.
First run() it directly.

now start the thread using start()

See the difference
Hope this helps,
Vanitha
Vedhas Pitkar
Ranch Hand

Joined: Jan 27, 2001
Posts: 445
u have just created 3 threads and put them in an infinite loop.For all 3threads to get c.p.u time put Thread.sleep()inthe while loop s.t. the thread gives time for the other two to execute.Correct me if i am wrong.
Vedhas
james hoskins
Ranch Hand

Joined: Jun 28, 2001
Posts: 31
You can use Thread.yield() causing a Thread to move into a ready to run state, giving other Threads the opportunity to run. You could call Thread.sleep() which puts your Thread into sleep state, but you don't need to in this instance.
Kind regards,
james hoskins.
Nisheeth Kaushal
Ranch Hand

Joined: Jul 20, 2001
Posts: 87
Hi Friends,
Great discussion, i was week in thread but slowly the concept is getting clear.
Thanx.
Nisheeth.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread
 
Similar Threads
Thread with infinite loop
won't it result in unreachable code?
Threads
threads
Threads