This week's giveaway is in the Programmer Certification (OCPJP) forum.
We're giving away four copies of Java Mock Exams (software) and have David Mayer on-line!
See this thread for details.
Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Practical Applications of Threads

 
Nick Sharp
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been learning about threads and I understand the concept behind them and the theory (or at least I think I do), but how to apply them in code still confuses me a little. Can anyone point me to some examples of how to use threads in different situations?

Here's a specific question:

Should I make a thread that instantiates an object that does stuff, or do should I make the thread itself do the stuff with its runnable? Does it matter aside from slight performance differences?
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 35977
422
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick Sharp wrote:Here's a specific question:

Should I make a thread that instantiates an object that does stuff, or do should I make the thread itself do the stuff with its runnable? Does it matter aside from slight performance differences?

It matters from a design perspective. It's clearer if a thread is limited in what it does and any significant logic resides elsewhere. Which makes it easier to reuse that logic.
 
Nick Sharp
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if a thread creates an object, anything that object does will take its turn in the JVM through the thread, correct? Could I call on an object created by a thread from outside the thread?
 
Campbell Ritchie
Marshal
Posts: 53133
124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is lots about threading on the net. Start with the Java™ Tutorials.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!