• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice question can anybody explain...
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By simply call the run() method, you didn't start the Thread. That call has no difference with a normal method call.
Guoqiao
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,
Great discussion, i was week in thread but slowly the concept is getting clear.
Thanx.
Nisheeth.
 
reply
    Bookmark Topic Watch Topic
  • New Topic