0% found this document useful (0 votes)
20 views3 pages

Java Thread Management Guide

The document discusses different methods in Java that allow threads to voluntarily give up CPU time, including sleep(), wait(), yield(), and join(). It also provides an example demonstrating the use of these methods and how they can change a thread's state. Finally, it discusses synchronized blocks and how they are used to synchronize access to shared resources across multiple threads.

Uploaded by

Pooja Vyas
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)
20 views3 pages

Java Thread Management Guide

The document discusses different methods in Java that allow threads to voluntarily give up CPU time, including sleep(), wait(), yield(), and join(). It also provides an example demonstrating the use of these methods and how they can change a thread's state. Finally, it discusses synchronized blocks and how they are used to synchronize access to shared resources across multiple threads.

Uploaded by

Pooja Vyas
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
You are on page 1/ 3

Threads can go to the non-runnable state by choice. Well, by the programer’s choice.

Let’s have a look at the methods available to us to give-up CPU time by choice:
 sleep(long millis) — This method causes the calling thread to sleep for the duration provided as the
parameter. It is important to note that on calling sleep, the thread gives up CPU time but does not release
locks on objects. After the sleep duration, the thread moves back to the runnable state waiting for the
scheduler to pick it up for execution.

 wait() or wait(long timeout) — This method causes the thread to give up CPU time as well as release any
object locks. It may or may not be called with the timeout parameter. When called without timeout, the
thread remains in the non-runnable state endlessly until another thread calls the notify() or notifyAll()
method, and when called with the timeout parameter, it waits for at most the timeout duration, and then
automatically moves to runnable state. This method is to be used in situations where multiple threads need
to work in synchronicity.

 yield() — This method is like a notification to the scheduler, that the thread is ready to give up execution.
The scheduler then decides, based on other live threads and their priorities, if it wants to move the calling
thread to runnable and give the CPU time to other threads, or keep running the existing thread. If we know
that a method/function will take a lot of time to execute, and that work is not urgent, we can write it with
strategically placed calls to the yield method, so that the scheduler can use the CPU for executing threads
with higher priority and shorter execution time.

 join() — join is called to pause the execution of the program until the thread calling the join method is
terminated.

class Thread1 implements Runnable{


@Override
public void run() {
System.out.println("We are inside the run() function."
+ "The thread is RUNNING ");
}
}

public class Main{


public static void printThreadState(Thread threadToCheck) {
System.out.println("The thread is in the \"" +
threadToCheck.getState() + "\" state.");
}
public static void main(String[] args) {
Thread t1 = new Thread(new Thread1());
printThreadState(t1);
t1.start();
printThreadState(t1);
// after run method this loop executes to ensure the run method
finished
for(int i=0; i<=10000; i++) {
for(int j=0; j<=10000; j++) {

}
}
printThreadState(t1);
}
}

The thread is in the "NEW" state.


The thread is in the "RUNNABLE" state.
We are inside the run() function.The thread is RUNNING
The thread is in the "TERMINATED" state.

Synchronized keyword in Java


The process of allowing only a single thread to access the shared data or resource at a particular point of
time is known as Synchronization. This helps us to protect the data from the access by multiple threads. Java
provides the mechanism of synchronization using the synchronized blocks.

Synchronized blocks in Java are marked with the synchronized keyword. A synchronized block
in Java is synchronized on some object.
class counter
{
int count;
public synchronized void incr()
{
count++;
}
}
public class Main
{
public static void main(String args[])
{
counter c = new counter();
Thread S1 =
new Thread( () -> {
for(int i=0;i<1000;i++){
c.incr();
}
});
Thread S2 =
new Thread( () -> {
for(int i=0;i<1000;i++){
c.incr();
}
});
// Start two threads of ThreadedSend type
S1.start();
S2.start();
// wait for threads to end
try
{
S1.join();
S2.join();
}
catch(Exception e)
{
System.out.println("Interrupted");
}
System.out.println("count" + c.count);
}
}

count2000

You might also like