JNA: use one thread for all callbacks

I have a situation where I use JNA with some native code with callbacks, and I notice that for each callback, the main thread of the native code forces it to handle the callback for the new java thread. Is there a way with JNA to indicate which thread is associated with callbacks so that I can get them all in one thread? I am currently receiving thousands of new threads every few seconds, and this is quite a resource.

+4
source share
1 answer

After a lot of digging, I found the right answer. I created a CallbackThreadInitilizer and used the Native.addCallbackThreadInitilizer method to associate it with each callback. This put them in one thread. If you have a ready-made answer, or you have some kind of flag that you send to indicate that this callback is the last, you can call Native.detatch (true) in this case, to disconnect the stream and let it be cleared gc. If you do not, it seems that you hold on for some time, and sometimes do new ones and close the old ones. I have never seen him hold more than three times.

+3
source

Source: https://habr.com/ru/post/1657808/


All Articles