• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is There a Java Exception that Gets Thrown when a Computer Shuts Down?  RSS feed

 
Kevin Simonson
Ranch Hand
Posts: 223
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Java program that has been converting a very large number from hexadecimal (55,732,656 digits) to its decimal equivalent. The only algorithm I know for doing that is O(n^2), so my program has been running for about four days, and it was only about 40% done an hour ago. A half hour ago something went wrong with my keyboard, and when I pressed a key, any key, nothing happened. I couldn't edit the word file I was working on; I couldn't type a command or any input into any of my command windows; I couldn't do much of anything. So I restarted my computer. That fixed the problem with the unresponsive keyboard; now I can type and have what I type show up in the window I'm working on. But the downside is that I lost all four day's worth of calculation, translating my huge hexadecimal number into decimal.

Is there by any chance a Java exception that gets thrown when the computer starts to do a shutdown prior to a restart, that I could catch and handle by writing my intermediate data to disk, so that all my work on a program that is likely to last for ten or so days doesn't get lost? It'd be nice if I could write an exception handler into my code that keeps me from losing four days work like I just did.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if this would work for you but for some long running problems I subdivide the problem into manageable pieces and write the partial results to a file(s). At the end I read back all the files and combine them into the final result. I write the program in such a way that if the program aborts it only has to pick up where it left off.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If it is a forced shutdown, either via the kill signal, or hardware shutdown, then "no", there is no way to continue running -- in order to clean everything up.  If it is a termination signal, like a control-c, then "yes", you can add a shutdown hook. Take a look at the Runtime class regarding this.

Henry
 
Kevin Simonson
Ranch Hand
Posts: 223
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:I don't know if this would work for you but for some long running problems I subdivide the problem into manageable pieces and write the partial results to a file(s). At the end I read back all the files and combine them into the final result. I write the program in such a way that if the program aborts it only has to pick up where it left off.

That's good advice, and I just implemented it in my code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!