0% found this document useful (0 votes)
0 views1 page

Thread

Synchronization in Java is crucial for managing multiple threads and preventing errors like thread interference and memory consistency issues, achieved through the 'synchronized' and 'volatile' keywords. Additionally, thread priority in Java ranges from 1 to 10, with methods available to get and set thread priority. The default priority is 5, while the minimum and maximum priorities are 1 and 10, respectively.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views1 page

Thread

Synchronization in Java is crucial for managing multiple threads and preventing errors like thread interference and memory consistency issues, achieved through the 'synchronized' and 'volatile' keywords. Additionally, thread priority in Java ranges from 1 to 10, with methods available to get and set thread priority. The default priority is 5, while the minimum and maximum priorities are 1 and 10, respectively.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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.

You might also like