• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interrupts handling.  RSS feed

 
Nick Widelec
Ranch Hand
Posts: 226
Eclipse IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Directly from this java tutorial on threads' interrupts: http://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html.



What if a thread goes a long time without invoking a method that throws InterruptedException? Then it must periodically invoke Thread.interrupted, which returns true if an interrupt has been received. For example:

for (int i = 0; i < inputs.length; i++) {
heavyCrunch(inputs[i]);
if (Thread.interrupted()) {
// We've been interrupted: no more crunching.
return;
}
}


What does it mean "if a thread goes a long time without invoking a method that throws InterruptedException? Then It must periodically invoce Thread.interrupted"?

I mean if the thread gets interrupted, the situation will be handled in the try-catch clause as being a checked exception it's mandatory to include it in a try-catch clause or declare it in the method signature.

Could somebody enlighten me on the subject please? thanks in advance.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick Widelec wrote:
What does it mean "if a thread goes a long time without invoking a method that throws InterruptedException? Then It must periodically invoce Thread.interrupted"?

I mean if the thread gets interrupted, the situation will be handled in the try-catch clause as being a checked exception


But calling interrupt() won't just cause the thread to throw InterruptedException out of nowhere. Only certain methods throw that exception. If I'm not calling those methods, and I'm just doing other processing that takes a long time, I won't ever know that I've been interrupted, unless I call the interrupted() method to test for it. And even if I am calling those methods and handling the exception, if, as that quote says, I'm doing a lot of stuff between calls to those methods that can throw that exception, I still need to check.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick Widelec wrote:Could somebody enlighten me on the subject please? thanks in advance.

Nick, I don't want to silence natural curiosity, but I wonder how much of this is going to do you much good at the moment (and I'm assuming you're fairly new to Java).

Multi-threaded programming - even with all the goodies given you by the Java language - is quite involved, and actually quite rare. At this stage, I'd suggest that learning how to use the language basics and becoming a good programmer are more important goals.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Multi-threaded programming [...] actually quite rare.


I think you and I must have different definitions of "rare". Other than a handful of one-off batch data collection tools, I can't remember ever working on a real Java app that didn't involve a lot of multithreading.

I do, however, 100% agree with your advice to focus on the fundamentals first.
 
Nick Widelec
Ranch Hand
Posts: 226
Eclipse IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Planning to sit for the OCJP 7 exam and multi-threading is an important topic of the exam.
I am quite good with the basics of the language, having passed OCAJP (if you do not have a good grasp of the basics you don't pass).

I am one of those who reckon the certification a good business card for freshers. And to whom says not http://www.reed.co.uk/jobs/junior-java-developer/23046503#/jobs?keywords=java%20certified
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick Widelec wrote:I am one of those who reckon the certification a good business card for freshers. And to whom says not http://www.reed.co.uk/jobs/junior-java-developer/23046503#/jobs?keywords=java%20certified


I can't speak for others, but my experience interviewing candidates with Java certs on their resumes has been almost entirely negative, to the point that I'm probably tougher on them than on those without. I'm not familiar with the OCAJP, but when I was interviewing folks with SCJP, it was obvious that they crammed for how to pass the exam, but didn't actually know much about the fundamentals of Java.

I'm not saying that's the case with you. I'm just warning you not to expect employers to put much stock on those certs, at least not if you're in the U.S.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:I think you and I must have different definitions of "rare". Other than a handful of one-off batch data collection tools, I can't remember ever working on a real Java app that didn't involve a lot of multithreading.

Hmmm. Really? Actually creating new Threads or Runnables and managing their execution? You surprise me. What biz are you in, if you don't mind me asking?

I've often had to create classes that were Thread-safe; but I regard that as "the opposite side of the coin" (and actually more fun, IMO).

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Jeff Verdegan wrote:I think you and I must have different definitions of "rare". Other than a handful of one-off batch data collection tools, I can't remember ever working on a real Java app that didn't involve a lot of multithreading.

Hmmm. Really? Actually creating new Threads or Runnables and managing their execution?


As the years have whizzed by, due to both the availability of higher level abstractions and the different nature of the projects I've worked on, I find myself doing much less of that than I used to, certainly. So perhaps it's not so much "rare" as "multithreading" where our definitions diverge. Even though I don't do much direct thread creation and management these days, I still have to be aware of what's shared and what's not, what I might need to synchronize, what might be running in one kind of thread vs. another, etc. And while the higher level stuff in java.util.concurrent.* frees us from a lot of the drudgery and some of the more error-prone aspects of the whole undertaking, it's by no means a panacea; we still need knowledge of Java's multithreading model. So I would say multithreading continues to be part of my daily life.

You surprise me. What biz are you in, if you don't mind me asking?


At the moment most of my work is on a couple of Android apps, but I also have some lesser server side duties. And yes, multithreading (at least as I use the term ) is hulking in the corner like a rhinoceros in the kitchen in both of these contexts.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!