Home:ALL Converter>Is java native method calls are Atomic calls?

Is java native method calls are Atomic calls?

Ask Time:2011-08-22T15:29:09         Author:SmartSolution

Json Formatter

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.

Author:SmartSolution,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/7144323/is-java-native-method-calls-are-atomic-calls
Michael Kutschke :

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.\n\nThe 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.\n\nIf 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.",
2011-08-22T11:48:18
Peter Lawrey :

To prevent an interrupt you must the the machine code instructions cli and sti to disable and enable interrupts (on x86/x64). It not something you can even do in plain C language.\n\nThis is so low level as it is rarely done. Largely because its rarely very useful. The main concern is usually the behaviour of interaction with other threads which is why Atomic is defined this way for most use cases, not atomic in terms of interrupts.",
2011-08-22T07:55:35
yy