Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is java native method calls are Atomic calls?

I need certain operation to be Atomic.

Here 'Atomic' means to say "once that method started execution, it shouldn't be interrupted till it finishes, And even thread scheduler also should not make that thread to Runnable state once its in Running state".

All methods are Native methods and multiple threads are running concurrently to execute these methods.

First Question: Is the native methods execution is Atomic in nature?

Second Question : To achieve the Atomicity, Can we use java concurent/lock APIs, if yes, please provide any example/link if possible?

Thanks.

like image 333
SmartSolution Avatar asked Jan 21 '26 16:01

SmartSolution


1 Answers

If i understand the question right, you are asking for: Is it possible for me to achieve that a certain thread will always be running on the CPU until it is finished? To be clear, it should not be replaced by another thread during that time.

The answer is: Even if it was (and i don't think it is), this would have nothing to do with atomicity. Because if you have multiple CPU's, multiple Threads can still access and change the same data while all are truely running uninterrupted at the same time. Therefore, even with your definition of "atomicity", you would still end-up with concurrency problems.

If you just want thread-safety in the conventional sense, then yes, you can use java-locks etc. to achieve that, even if you call native methods. see http://journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/sync.html for an example of java thread synchronization from within native methods.

like image 178
Michael Kutschke Avatar answered Jan 23 '26 05:01

Michael Kutschke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!