Keywords volatile, synchronized, transient, native in Java

volatile - this modifier forces threads to disable access optimization and use a single instance of the variable. If the variable is of primitive type, this will be enough to ensure thread safety. If the variable is a reference to an object, only the value of this reference will be synchronized. However, the data contained in the object will not be synchronized!

synchronized is a reserved word that allows you to achieve synchronization in methods or code blocks that are tagged with it.

The transient and native keywords have nothing to do with multithreading, the first is used to indicate class fields that do not need to be serialized, and the second is used to signal that the method is implemented in platform-dependent code.

Differences between volatile and Atomic variables

volatile enforces a single instance of a variable, but does not guarantee atomicity. For example, count++ will not become atomic simply because count is declared volatile. On the other hand, the AtomicInteger class provides an atomic method to perform such complex operations atomically, for example getAndIncrement() is an atomic replacement for the increment operator, it can be used to atomically increment the current value by one. Atomic versions for other data types are constructed in a similar way.

Differences between java.util.concurrent.Atomic*.compareAndSwap() and java.util.concurrent.Atomic*.weakCompareAndSwap()

  • weakCompareAndSwap() does not create a memory barrier and does not guarantee happens-before
  • weakCompareAndSwap() is highly cache/CPU dependent, and may return false for no apparent reason
  • weakCompareAndSwap(), lighter, but not supported by all architectures and not always efficient operation

Thread priority

Thread priorities are used by the thread scheduler to make decisions about when which thread will be allowed to run. In theory, high-priority threads receive more CPU time than low-priority threads. In practice, the amount of processor time a thread receives is often dependent on several factors besides its priority.

To set the priority of a thread, use the method of the Thread class: final void setPriority(int level). The level value ranges from Thread.MIN_PRIORITY = 1 to Thread.MAX_PRIORITY = 10. The default priority is Thread.NORM_PRlORITY = 5.

You can get the current value of the thread priority by calling the: final int getPriority() method on an instance of the Thread class.

Daemon threads

Daemon threads run in the background with the program, but they are not an integral part of the program. If a process can run against the background of the main threads of execution and its activity is to serve the main threads of the application, then such a process can be started as a daemon thread using the setDaemon(boolean value) method called on the thread before it was started. The boolean isDaemon() method allows you to determine whether the specified thread is a daemon or not. The basic property of daemon threads is the ability of the main application thread to terminate the execution of the daemon thread (as opposed to normal threads) with the end of the main() method code, regardless of the fact that the daemon thread is still running.

Can the main thread of a program be made a daemon?

No. Daemon threads allow you to describe background processes that are needed only to service the main threads of execution and cannot exist without them.

What does it mean to "sleep" the thread?

This means to suspend it for a certain period of time by calling during its execution the static method Thread.sleep() passing the required amount of time in milliseconds as a parameter. Before this time expires, the thread can be brought out of the wait state by calling interrupt(), throwing an InterruptedException.


Read also:


Comments

Popular posts from this blog

Methods for reading XML in Java

XML, well-formed XML and valid XML

ArrayList and LinkedList in Java, memory usage and speed