• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

threads

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the code below we are directly calling the
run() method.how is this possible?? i thought
we have to call the start() method in order
to call run()
please explain...

class a1 extends Thread
{
public static void main(String args[])
{
a1 a =new a1();
Thread a11 = new Thread(a);
Thread a12 = new Thread(a);
a11.run();
a12.run();
}
int x=1,y=1;
synchronized void method1()
{
x =x+1;
y=y+1;
}
public void run()
{
for(int i =0 ; i<5;i++)
{
method1();
System.out.println(x+""+y);
}
}
}
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear prasanthi,
It is very much valid to call run() on thread without calling start()..........but it is not advised to do so........Pls donot ask me why??
Harpal
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The Java Tutorial says that
"The start method creates the system resources necessary to run the thread, schedules the thread to run, and calls the thread's run method". So, for an orderly processing of the thread, it is better to call start() method than calling the run() method.
Aparna
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think Aparna's is write.
You are here just overridding the run() method and calling it as you would have called any normal overridden method.
start() method is neccessary to create the resources....
Thanks Aparna.....

See these type of questions well before sitting for the exam...
All the best to you all..
Amit Tyagi
 
He was giving me directions and I was powerless to resist. I cannot resist this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic