0% found this document useful (0 votes)
11 views

Multithreading Part2

multithreading

Uploaded by

R R
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Multithreading Part2

multithreading

Uploaded by

R R
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

RU JAVA PROGRAMMING (C7) UNIT-IV

Thread Prioritization
• Each thread in Java has a priority.
• Priority is represented by a number between 1 and 10.
• Three priority constants are defined in Thread class as follows

Constant Value
NORM_PRIORITY 5

MIN_PRIORITY 1

MAX_PRIORITY 10

• In most cases, thread scheduler schedules the threads according to their priority
(known as pre-emptive scheduling). But it is not guaranteed because it depends on
JVM specification that is which scheduling it chooses.
• setPriority() method is used to set thread priority value.
• getPriority() method is used to return priority value.

Question: Run thread A B and C concurrently with different priority. Print their
corresponding name and priority also when they are running. Thread A, B and C
must sleep for 1000ms, 5000ms, 1000 ms respectively.

©UMESH PRASAD ROUT@2023 1


RU JAVA PROGRAMMING (C7) UNIT-IV

Output:

Thread Sleeping in Java

The Thread class provides two methods for sleeping a thread:

o public static void sleep(long miliseconds)throws InterruptedException


o public static void sleep(long miliseconds, int nanos)throws InterruptedException

Sleep Method Demo Program

Suspending and Resuming Threads


• The suspend () method of thread class puts the thread from running to waiting state.
• This method is used if you want to stop the thread execution and start it again when a
certain event occurs.
• This method allows a thread to temporarily cease execution.
• The suspended thread can be resumed using the resume() method.

Signature of suspend ()

public final void suspend()

• resume() method is used to resume a thread which was suspended using suspend()
method.
• This method allows the suspended thread to start again.

Signature of suspend ()

public final void resume()

©UMESH PRASAD ROUT@2023 2


RU JAVA PROGRAMMING (C7) UNIT-IV

• Both suspend() and resume() are deprecated method in Java. Hence the program shows
warning at compile time.

Demo Program for Thread suspension and resumption

Thread Synchronization
Synchronization is the capability of control the access of multiple threads to any shared
resource. Synchronization is needed in case we want only one thread can access the shared
resource at a time. In other words synchronization achieves mutual exclusion among multiple
concurrent threads.
The synchronization is mainly used to
o To prevent thread interference.
o To prevent inconsistency problem.

Mutual Exclusion helps keep threads from interfering with one another while sharing data.
This can be done by the following ways in java:

1. by synchronized method
2. by synchronized block

Synchronized Method
• If synchronized keyword is used before method signature, it is known as synchronized
method.
• Synchronized method is used to lock an object for any shared resource.
• When a thread invokes a synchronized method, it automatically acquires the lock for that
object and releases it when the method returns

©UMESH PRASAD ROUT@2023 3


RU JAVA PROGRAMMING (C7) UNIT-IV

Synchronized Method Demo Program

Output (with synchronization)

Output (without synchronization)

©UMESH PRASAD ROUT@2023 4

You might also like