-2

I know I can add a shutdown hook which will run in a new Thread, but how can I add a shutdown hook for the current thread, if even possible? I need some code to be run in the same thread as which created the shutdown hook but I couln't find an answer yet.

edit: some background information:

I'm writing a library which calls CoInitialize from MS COM interface whenever a specific object is created. I want the user to not need to care about uninitializing COM thus I thought I could simply call CoUninitialize in a shutdown hook. But the uninitialization must happen from the same thread as from which it was initialized, so it doesn't work with a shutdown hook.

SimonH
  • 1,385
  • 15
  • 35
  • The shutdown process belongs to the jvm. What you are asking does not seem feasible. A shutdown hook is just a runnable that the jvm launches prior to exiting – bichito Jun 20 '17 at 15:21
  • What? Develop your question. You could just use a while if you want to delay a shutdown. – Turtle Jun 20 '17 at 15:21
  • Would a `finally` block do what you want? – Andy Turner Jun 20 '17 at 15:23
  • @Andy_Turner no, I'm developing a library and thus can't force the user to wrap everything in a try block – SimonH Jun 20 '17 at 15:27
  • Com interface like MS COM? Do you have that running in a java thread? – bichito Jun 20 '17 at 15:28
  • @efekctive yes, using JNA – SimonH Jun 20 '17 at 15:30
  • I think you are in a lot more trouble than you think. Maybe wrap the JNA call in a try block. Interrupt that thread from a shutdown hook and clean it in the exception handling. If this works let me know I will post it as answer – bichito Jun 20 '17 at 15:38
  • @efekctive That wouldn't work as I want the user to not care about threading (which would be a horrible library otherwise). But when I initialize COM from another thread internally, the user wouldn't be able to use COM from his own thread. – SimonH Jun 20 '17 at 15:45
  • I think you are in a mess. You need a try/block to interrupt user's thread from shutdown to avoid creating leaks and other problems. But hopefully I am wrong – bichito Jun 20 '17 at 15:55
  • @efekctive yeah well, imo calling com from java is messy altogether :) I'll see if I can find another way around.. – SimonH Jun 20 '17 at 16:02
  • What about using nio/mapped byte buffer between com and jvm? I don't know if what you need from COM can be easily put into bytes but it is worth a look. It seems that the main project is in java so using C# is not an option. – bichito Jun 20 '17 at 17:36
  • @efekctive jna handles the communication between com and jvm quiet well, leaves the user with a high level api. Thus I'd prefer to not hack into jna's code introducing bugs etc. I can't imagine how modifying the communication would solve my problem anyways since it's already just one method call what causes the problem – SimonH Jun 21 '17 at 08:31
  • It would be no jna. sending requests to COM via a memory mapped file using the same user api. COM lives in a non jvm thread so no mess. It would be a rewrite/departure of what you have. Good luck – bichito Jun 21 '17 at 11:58

1 Answers1

1

Runtime.addShutdownHook...

Registers a new virtual-machine shutdown hook.

(my bold, see API here).


Dissecting your questions...


[...]how can I add a shutdown hook for the current thread

There is no such thing as a shutdown hook for any given thread.

You can either simply write the statements you require executing by the end of the run implementation, or use a finally statement to guarantee their execution after a try (and/or a catch) statement is executed, within your given thread's run implementation.

I need some code to be run in the same thread as which created the shutdown

That is a different question altogether. If you want execution within the same thread creating the shutdown hook, simply add the statements after your Runtime.getRuntime().addShutdownHook(someThread); statement.

Mena
  • 47,782
  • 11
  • 87
  • 106
  • He can not add an already running thread to the shutdown pool. But I may be wrong – bichito Jun 20 '17 at 15:33
  • @efekctive I didn't quite understand the question your way... what is the rationale for this interpretation? – Mena Jun 20 '17 at 15:35
  • Read he is comments – bichito Jun 20 '17 at 15:36
  • I can't add the statements after the `addShutdownHook` since they would be run instantly after the hook was added, but I want them to be run at jvm shutdown, without having a try-finally block – SimonH Jun 20 '17 at 15:39
  • @LPrc but why don't you just add them to the `Thread` you're passing to `addShutdownHook` then? – Mena Jun 20 '17 at 15:45
  • @Mena because the uninitialization of COM needs to happen in the same thread as from which it was initialized. So by creating a new Thread when adding a shutdown hook it wouldn't uninitialize COM correctly (see my edit) – SimonH Jun 20 '17 at 15:47
  • @LPrc not familiar with COM but you **cannot** add a shutdown hook with a thread that's already running (think initialization of the external process inline with the `run` method, then waiting on some monitor to "proceed" to uninitialization once signaled). So, I can't really think of a solution involving a shutdown hook here. – Mena Jun 20 '17 at 16:13
  • @Mena okay, thanks a lot for your effort :) then I can just leave this idea behind and maybe find another solution – SimonH Jun 20 '17 at 16:20
  • @LPrc no problems, sorry that didn't help. – Mena Jun 20 '17 at 16:23