Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Can we only start() a thread?

 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that we can also directly invoke the run method of the thread like so:
myThread.run();
After all, the start method also simply calls run(), right?
But look at this. This problem is from www.javacertificate.com


Which of the following statements are true? [Check all correct answers]
1 A thread is a different program.
2 A thread can share data with other threads.
3 A thread is started by calling the start method.
4 A thread is started by calling the run method.


javacertificate says only 2 and 3 are correct. I think 2 and 3 and 4 are all correct.
This is what javacertificate.com says:


The only correct way to start a thread is calling the start method of the Thread class.


Do you people agree?
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think that the start() method only executes the run() method? Did you actually take a look at the ocde that the start() method invokes?
Here is the code:
public synchronized native void start();
So there is obviously something else going on than just executing the run() method. In fact, there is a lot more going on.
If you just run the run() method it does not create a separate thread. Only when you run the start() method does it create a separate thread.
 
Mark Lau
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, OK, I gotcha. I was suspecting, though.
So javacertificate is actually right, right?
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the proper way to start a thread is with the start() method.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Your mother was a hamster and your father was a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic