Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Interrupts vs OS Interrupts  RSS feed

 
John Pradeep.v
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,
I just want to know how Java interrupt handling works internally...?

Generally, looking at the operating system perspective, we know that when an interrupt occurs, the interrupt service routine is executed (from interrupt vector table) where the ISR does some operation and the process interrupted resumes
Does it imply that java internally have its own custom ISR registered in the interrupt vector table? how does it work?

If my above question is clarified, then how does Thread.sleep() work internally? again from my little knowledge about O.S concept when a process/thread sleeps it enters a waiting channel (WCHAN) and the timer interrupt is handled to decide to wake up a thread sleeping in WCHAN
Said that, how does Thread.sleep(), interrupt, InterruptedException all related with the operating system? i am a bit confused relating all these...

Any kind of help or guidance is much appreciated!!

Thanks in Advance,
John
 
Henry Wong
author
Sheriff
Posts: 22837
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With all modern JVMs, the threading apis are implemented on top of the native apis (POSIX threads, Solaris threads, Windows threads, etc). So, under the covers, there may be a call that goes to a native API, which in turn, goes to an OS API, and which in turn, is serviced using interrupts.... But for the most part, there is no relationship between an OS interrupt and a Java Interrupt.

Not sure what you mean by a jump table (actually I do)... but Java interrupts don't have a concept of interrupt / signal numbers -- you are simply interrupting a thread.

Henry
 
John Pradeep.v
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote: But for the most part, there is no relationship between an OS interrupt and a Java Interrupt.



Thanks Henry...

I am aware that The java threads are managed by the operating system as light weight processes, this lead me to think that the interrupt handling mechanism which java provides is some kind of intermediate layer to the actual mechanism provided by the operating system.

so do you mean that java threads are managed by operating system but the interrupt handling which is provided by java library is purely maintained by JVM rather than the O.S?

John
 
Henry Wong
author
Sheriff
Posts: 22837
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Pradeep.v wrote:
so do you mean that java threads are managed by operating system but the interrupt handling which is provided by java library is purely maintained by JVM rather than the O.S?


No. I merely meant that besides having the "interrupt" in the name, there is little relationship between the two mechanism. And comparing them as such doesn't really make much sense.

OS interrupts are triggered by hardware (although, on many processors it can be program triggered). OS interrupts code run in kernal mode. And after the interrupt code is run, the original code can run where it left off.

Java "interrupts" may not interrupt at all. In many cases, it will just set a flag which can be checked by the interrupted thread. In the cases, where it is "interrupted", via an exception, it is not capable of starting where it left off -- as an exception is thrown.

Henry
 
John Pradeep.v
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Java "interrupts" may not interrupt at all. In many cases, it will just set a flag which can be checked by the interrupted thread. In the cases, where it is "interrupted", via an exception, it is not capable of starting where it left off -- as an exception is thrown.



okay, I think i got the point!!

but just one last confusion still ringing in my mind - when a Thread.sleep() is called, it goes to sleep... when another thread calls interrupt on the sleeping thread it throws an InterruptedException, i believe when a thread goes to sleep it actually goes to the sleep state which is one of the states of a thread the operating system maintains...?
if this is true, then on interrupting (java way of interrupting), has to wake up the sleeping thread, I think here it is more than setting the flag... could you please let me know how the JVM handles a interrupt request for a sleeping thread?

Regards,
John
 
Henry Wong
author
Sheriff
Posts: 22837
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Pradeep.v wrote:
but just one last confusion still ringing in my mind - when a Thread.sleep() is called, it goes to sleep... when another thread calls interrupt on the sleeping thread it throws an InterruptedException, i believe when a thread goes to sleep it actually goes to the sleep state which is one of the states of a thread the operating system maintains...?
if this is true, then on interrupting (java way of interrupting), has to wake up the sleeping thread, I think here it is more than setting the flag... could you please let me know how the JVM handles a interrupt request for a sleeping thread?


This is obviously an implementation detail -- and probably differs from platform to platform. This means that if you are interested, you will need to look at the source code yourself. For example, if the JVM uses POSIX as the underlying threading API, I would guess that it probably uses a sigalarm to wake up from a sleep, but there is nothing in the spec that says it must be done that way.

Henry
 
John Pradeep.v
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
This is obviously an implementation detail -- and probably differs from platform to platform. This means that if you are interested, you will need to look at the source code yourself. For example, if the JVM uses POSIX as the underlying threading API, I would guess that it probably uses a sigalarm to wake up from a sleep, but there is nothing in the spec that says it must be done that way.

Henry


Thanks Henry.. i will sure have a look at the Spec or any details about the implementation.

John
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!