What is Synchronization in Java
Synchronization in Java is an important concept since Java is a multi-
threaded language where multiple threads run in parallel to complete
program execution. In multi-threaded environment synchronization of Java
object or synchronization of Java class becomes extremely important.
Synchronization in Java is possible by using Java
keywords "synchronized" and "volatile”.
Concurrent access of shared objects in Java introduces to kind of errors:
thread interference and memory consistency errors and to avoid these errors
you need to properly synchronize your Java object to allow mutual exclusive
access of critical section to two threads.
Example:
Java Thread Priority in Multithreading
In a Multi threading environment, thread scheduler assigns processor to a thread based on priority of
thread. Whenever we create a thread in Java, it always has some priority assigned to it. Priority can
either be given by JVM while creating the thread or it can be given by programmer explicitly.
Accepted value of priority for a thread is in range of 1 to 10. There are 3 static variables defined in
Thread class for priority.
public static int MIN_PRIORITY: This is minimum priority that a thread can have. Value for this is 1.
public static int NORM_PRIORITY: This is default priority of a thread if do not explicitly define it. Value
for this is 5.
public static int MAX_PRIORITY: This is maximum priority of a thread. Value for this is 10.
Get and Set Thread Priority:
1. public final int getPriority(): [Link]() method returns priority of given
thread.
2. public final void setPriority(int newPriority): [Link]() method changes the
priority of thread to the value newPriority. This method throws IllegalArgumentException if
value of parameter newPriority goes beyond minimum(1) and maximum(10) limit.