File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Thread Switching - any special method/code it calls before a switch is made? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread Switching - any special method/code it calls before a switch is made?" Watch "Thread Switching - any special method/code it calls before a switch is made?" New topic
Author

Thread Switching - any special method/code it calls before a switch is made?

Dinesh Andavar
Greenhorn

Joined: Jun 29, 2013
Posts: 12
Hi Is there any method or code which the JVM calls by default before taking a thread out of the running state to say either running/blocked state???


Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1017
    
  15
Yea, I'm guessing there must be many methods JVM must call. For instance, JVM/OS has to store the values of the variables, save the thread state, update some global state variables, before the swap.

If you mean user created methods, I don't know. So I'd pass.



Edit - Hey, I must clarify one thing. I didn't mean the Java variables, when I said variables. I meant the registers and the OS variables.
For example some sort of a mechanism must tell the OS that when a thread starts running again, it must start from a particular instruction.
I meant those things when I said Thread state.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12781
    
    5
Dinesh Andavar wrote:Hi Is there any method or code which the JVM calls by default before taking a thread out of the running state to say either running/blocked state???


Nope - see the JavaDocs for java.lang.Thread for discussion of Thread life cycle.

Bill
Dinesh Andavar
Greenhorn

Joined: Jun 29, 2013
Posts: 12
Thanks for your feedback guys.
Would not it be great if there was a user created method that JVM would call before switching.
Do any of you guys see that as a useful feature or is it just me??!!
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Dinesh Andavar wrote:Thanks for your feedback guys.
Would not it be great if there was a user created method that JVM would call before switching.
Do any of you guys see that as a useful feature or is it just me??!!


That assumes the JVM is either in control of thread switching (which usually it is not) or privy to when said switching occurs. Switching thread contexts itself is pretty darn expensive as it is, but to have it have to process a queue of tasks to execute when switching threads would be even more so - and would just delay it. Not to mention - where (what thread) would this code be executed in? Should it interrupt the normal flow of the old thread? This sounds like a scary idea to me, as it seems like it could lead to all kinds of inconsistent states.

Is there any reason you think it would be useful?


Steve
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18829
    
  40

Steve Luke wrote:
That assumes the JVM is either in control of thread switching (which usually it is not) or privy to when said switching occurs. Switching thread contexts itself is pretty darn expensive as it is, but to have it have to process a queue of tasks to execute when switching threads would be even more so - and would just delay it. Not to mention - where (what thread) would this code be executed in? Should it interrupt the normal flow of the old thread? This sounds like a scary idea to me, as it seems like it could lead to all kinds of inconsistent states.

Is there any reason you think it would be useful?


Also, what is to prevent the application from using the callback to execute more code? ... meaning if I don't want the OS from ending my time slice, can't I just use the callback to extend it (execute what I wanted done in the original thread in the callback instead)?


Anyway, if you really want to do this, one option is to create your own scheduling system. You can have your threads do some work, and then check if it should switch out (using some sort of common state). This means that your threads are "context switch" aware -- and will cooperatively puts itself to sleep, if it decides that it's time slice is done. You can also put a call to your special method prior to the switch here, since it is your code.

The underlying OS scheduler only sees one runnable thread at a time, and doesn't know that your threads are doing cooperative user-level scheduling. It is merely running the only thread that is runnable at any given time.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Switching - any special method/code it calls before a switch is made?