• 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

What must a thread keep track of?

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read in HeadFirst Java that a thread must keep track of which statement (of which method)
is currently executing on the thread's stack..

How is it done and why is it necessary?
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't have a copy of this book, but I imagine they were making a point of how little information has to be saved to switch from thread to thread in contrast with switching between processes. That's because threads share the same memory space.

How is it done? Actually, I'm not totally sure, but basically it would be something like taking the value of the program counter register out of the CPU, and probably some other register values, and storing them in the thread's local memory area. Then the thread manager could read those values back to restore the CPU state and restart the thread.

Why is it done? A thread runs its task for some specified amount of time when the thread scheduler interrupts it and gives another thread a chance to run. Eventually, the first thread gets to run again, and it needs some way to pick up where it left off.
 
Rajiv Chelsea
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anybody who can shed more light on this
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajiv Chelsea wrote:...why is it necessary?



It is necessary because the OS can switch contexts, temporarily pausing execution of the current thread from running in favor of allowing a different one run. At a later time the Thread will be re-started and needs to know precisely where it was when it was stopped because of the Context Switch.


Rajiv Chelsea wrote:How is it done...?



That is an implementation detail of the particular JVM you are running, and is not really necessary to know the details of - especially since it could be different between OSes, versions, or vendors. Why do you think you need to know how?
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic