This presentation is about advanced multithreading and concurrency in Java. I have tried my best to explain the concepts with code. Feel free to reach me if you have any questions or concerns.
Lecture 10 from the IAG0040 Java course in TTÜ.
See the accompanying source code written during the lectures: https://github.com/angryziber/java-course
This presentation is about advanced multithreading and concurrency in Java. I have tried my best to explain the concepts with code. Feel free to reach me if you have any questions or concerns.
Lecture 10 from the IAG0040 Java course in TTÜ.
See the accompanying source code written during the lectures: https://github.com/angryziber/java-course
This presentation will give a brief idea about threads.
This presentation gives you what is required if you are a starter.
This has the lifecycle, multithreading and differences between multithreadind and normal threading.
This presentation even have example programs.
Threads : Single and Multitasking, Creating and terminating the thread, Single and Multi tasking
using threads, Deadlock of threads, Thread communication.
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfPeter Spielvogel
Building better applications for business users with SAP Fiori.
• What is SAP Fiori and why it matters to you
• How a better user experience drives measurable business benefits
• How to get started with SAP Fiori today
• How SAP Fiori elements accelerates application development
• How SAP Build Code includes SAP Fiori tools and other generative artificial intelligence capabilities
• How SAP Fiori paves the way for using AI in SAP apps
In his public lecture, Christian Timmerer provides insights into the fascinating history of video streaming, starting from its humble beginnings before YouTube to the groundbreaking technologies that now dominate platforms like Netflix and ORF ON. Timmerer also presents provocative contributions of his own that have significantly influenced the industry. He concludes by looking at future challenges and invites the audience to join in a discussion.
UiPath Test Automation using UiPath Test Suite series, part 4DianaGray10
Welcome to UiPath Test Automation using UiPath Test Suite series part 4. In this session, we will cover Test Manager overview along with SAP heatmap.
The UiPath Test Manager overview with SAP heatmap webinar offers a concise yet comprehensive exploration of the role of a Test Manager within SAP environments, coupled with the utilization of heatmaps for effective testing strategies.
Participants will gain insights into the responsibilities, challenges, and best practices associated with test management in SAP projects. Additionally, the webinar delves into the significance of heatmaps as a visual aid for identifying testing priorities, areas of risk, and resource allocation within SAP landscapes. Through this session, attendees can expect to enhance their understanding of test management principles while learning practical approaches to optimize testing processes in SAP environments using heatmap visualization techniques
What will you get from this session?
1. Insights into SAP testing best practices
2. Heatmap utilization for testing
3. Optimization of testing processes
4. Demo
Topics covered:
Execution from the test manager
Orchestrator execution result
Defect reporting
SAP heatmap example with demo
Speaker:
Deepak Rai, Automation Practice Lead, Boundaryless Group and UiPath MVP
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™UiPathCommunity
In questo evento online gratuito, organizzato dalla Community Italiana di UiPath, potrai esplorare le nuove funzionalità di Autopilot, il tool che integra l'Intelligenza Artificiale nei processi di sviluppo e utilizzo delle Automazioni.
📕 Vedremo insieme alcuni esempi dell'utilizzo di Autopilot in diversi tool della Suite UiPath:
Autopilot per Studio Web
Autopilot per Studio
Autopilot per Apps
Clipboard AI
GenAI applicata alla Document Understanding
👨🏫👨💻 Speakers:
Stefano Negro, UiPath MVPx3, RPA Tech Lead @ BSP Consultant
Flavio Martinelli, UiPath MVP 2023, Technical Account Manager @UiPath
Andrei Tasca, RPA Solutions Team Lead @NTT Data
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfPaige Cruz
Monitoring and observability aren’t traditionally found in software curriculums and many of us cobble this knowledge together from whatever vendor or ecosystem we were first introduced to and whatever is a part of your current company’s observability stack.
While the dev and ops silo continues to crumble….many organizations still relegate monitoring & observability as the purview of ops, infra and SRE teams. This is a mistake - achieving a highly observable system requires collaboration up and down the stack.
I, a former op, would like to extend an invitation to all application developers to join the observability party will share these foundational concepts to build on:
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Albert Hoitingh
In this session I delve into the encryption technology used in Microsoft 365 and Microsoft Purview. Including the concepts of Customer Key and Double Key Encryption.
Transcript: Selling digital books in 2024: Insights from industry leaders - T...BookNet Canada
The publishing industry has been selling digital audiobooks and ebooks for over a decade and has found its groove. What’s changed? What has stayed the same? Where do we go from here? Join a group of leading sales peers from across the industry for a conversation about the lessons learned since the popularization of digital books, best practices, digital book supply chain management, and more.
Link to video recording: https://bnctechforum.ca/sessions/selling-digital-books-in-2024-insights-from-industry-leaders/
Presented by BookNet Canada on May 28, 2024, with support from the Department of Canadian Heritage.
Elevating Tactical DDD Patterns Through Object CalisthenicsDorra BARTAGUIZ
After immersing yourself in the blue book and its red counterpart, attending DDD-focused conferences, and applying tactical patterns, you're left with a crucial question: How do I ensure my design is effective? Tactical patterns within Domain-Driven Design (DDD) serve as guiding principles for creating clear and manageable domain models. However, achieving success with these patterns requires additional guidance. Interestingly, we've observed that a set of constraints initially designed for training purposes remarkably aligns with effective pattern implementation, offering a more ‘mechanical’ approach. Let's explore together how Object Calisthenics can elevate the design of your tactical DDD patterns, offering concrete help for those venturing into DDD for the first time!
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...James Anderson
Effective Application Security in Software Delivery lifecycle using Deployment Firewall and DBOM
The modern software delivery process (or the CI/CD process) includes many tools, distributed teams, open-source code, and cloud platforms. Constant focus on speed to release software to market, along with the traditional slow and manual security checks has caused gaps in continuous security as an important piece in the software supply chain. Today organizations feel more susceptible to external and internal cyber threats due to the vast attack surface in their applications supply chain and the lack of end-to-end governance and risk management.
The software team must secure its software delivery process to avoid vulnerability and security breaches. This needs to be achieved with existing tool chains and without extensive rework of the delivery processes. This talk will present strategies and techniques for providing visibility into the true risk of the existing vulnerabilities, preventing the introduction of security issues in the software, resolving vulnerabilities in production environments quickly, and capturing the deployment bill of materials (DBOM).
Speakers:
Bob Boule
Robert Boule is a technology enthusiast with PASSION for technology and making things work along with a knack for helping others understand how things work. He comes with around 20 years of solution engineering experience in application security, software continuous delivery, and SaaS platforms. He is known for his dynamic presentations in CI/CD and application security integrated in software delivery lifecycle.
Gopinath Rebala
Gopinath Rebala is the CTO of OpsMx, where he has overall responsibility for the machine learning and data processing architectures for Secure Software Delivery. Gopi also has a strong connection with our customers, leading design and architecture for strategic implementations. Gopi is a frequent speaker and well-known leader in continuous delivery and integrating security into software delivery.
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf91mobiles
91mobiles recently conducted a Smart TV Buyer Insights Survey in which we asked over 3,000 respondents about the TV they own, aspects they look at on a new TV, and their TV buying preferences.
The Metaverse and AI: how can decision-makers harness the Metaverse for their...Jen Stirrup
The Metaverse is popularized in science fiction, and now it is becoming closer to being a part of our daily lives through the use of social media and shopping companies. How can businesses survive in a world where Artificial Intelligence is becoming the present as well as the future of technology, and how does the Metaverse fit into business strategy when futurist ideas are developing into reality at accelerated rates? How do we do this when our data isn't up to scratch? How can we move towards success with our data so we are set up for the Metaverse when it arrives?
How can you help your company evolve, adapt, and succeed using Artificial Intelligence and the Metaverse to stay ahead of the competition? What are the potential issues, complications, and benefits that these technologies could bring to us and our organizations? In this session, Jen Stirrup will explain how to start thinking about these technologies as an organisation.
Enhancing Performance with Globus and the Science DMZGlobus
ESnet has led the way in helping national facilities—and many other institutions in the research community—configure Science DMZs and troubleshoot network issues to maximize data transfer performance. In this talk we will present a summary of approaches and tips for getting the most out of your network infrastructure using Globus Connect Server.
Enhancing Performance with Globus and the Science DMZ
Multi threading
1. Programming in Java
Topic: Multi-threading
By
Ravi Kant Sahu
Asst. Professor
Lovely Professional University, Punjab
2. Agenda
•
•
•
•
•
•
•
•
•
•
Introduction
Process Vs Thread
Thread Life cycle
Runnable Interface
Thread class
Creating a Thread
Creating Multiple Threads
Synchronization of Threads
Inter-thread Communication
Suspending, Resuming and Stopping Threads
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
3. Introduction
• Concurrent Execution of multiple tasks is called Multitasking.
Multitasking
Multi-processing
(Process-based)
Multi-threading
(Thread-based)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
4. Introduction
• Multithreading is a specialized form of multitasking.
• A multithreaded program contains two or more parts (threads)
that can run concurrently.
• Each thread defines a separate path of execution.
• Java provides built-in support for multithreaded programming.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
5. Process and Threads
• In concurrent programming, there are two basic units of execution:
processes and threads.
• Process: is an executable program loaded in memory
» has its own Variables & data structures (in memory)
» Communicate via operating system, files, network
» May contain multiple threads
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
6. Threads
• A thread is an independent module of an application that can be
concurrently executed with other threads.
• Also known as “lightweight process”.
• Threads exist within a process — every process has at least one.
• Multiple threads in process execute same program.
• Threads share the process's resources, including memory and
open files.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
7. • A thread cannot exist on its own; it must be a part of a process.
• A process remains running until all of the non-daemon threads
are done executing.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
8. Life Cycle of a Thread
• New: The thread is in new state if you create an instance of
Thread class but before the invocation of start() method.
• Runnable: The thread is in runnable state after invocation of
start() method, but the thread scheduler has not selected it to
be the running thread.
• Running: The thread is in running state if the thread
scheduler has selected it.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
9. • Waiting/Blocked: This is the state when the thread is still
alive, but is currently not eligible to run.
• Terminated: A thread is in terminated or dead state when its
run() method exits.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
10. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
11. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
12. Multithreading in Java
• In order to achieve multithreading in java application,
java.lang.Runnable interface is provided.
• java.lang.Thread class is provided as part of core java library
which provides methods that are used to:
– start and suspend a thread
– obtain the state of a thread
– change the state of a thread
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
13. Runnable Interface
• Runnable interface contains a single method run().
public interface Runnable
{
public void run();
}
• Inside run( ), we will define the code that constitutes the new
thread.
• run( ) can call other methods, use other classes, and declare
variables.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
14. Thread Class
• Thread class provides methods to start a thread, suspend a
thread and obtain the state of a thread.
public class Thread {
public Thread(Runnable R); // Thread ⇒ R.run()
public Thread(Runnable R, String name);
public void start(); // begin thread execution
...
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
15. Methods of Thread class
• currentThread(): used to obtain the reference of thread
object for the current thread.
public static Thread currentThread()
• getName(): returns the name of the thread.
public String getName()
• setName(): used to change the name of a thread.
public void setName(String Name)
• getPriority(): used to obtain the priority of thread.
public int getPriority()
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
16. Methods of Thread class
• start(): used to start the execution of a thread.
public void start()
• sleep(): is used to suspend the current thread for the specified time.
public static void sleep(long milliseconds) throws InterruptedException
• isAlive(): used to find out whether a thread is completed or not .
public boolean isAlive()
• static void yield(): This method causes the currently executing
thread object to temporarily pause and allow other threads to execute.
• void join(): Waits for this thread to die.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
17. Creating Threads in Java
• A user thread is represented by run().
• There are two ways of defining a thread:
– By extending Thread class
– By implementing Runnable interface
Runnable
Runnable
Thread
MyThread
MyThread
• The first case is not recommended because our class may already
have a super class, so we can not extend another class.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
18. Implementing Runnable
• We can construct a thread on any object that implements Runnable.
• To implement Runnable, a class need only implement run( ).
• Inside run( ), we will define the code that constitutes the new thread.
• run( ) can call other methods, use other classes, and declare variables,
just like the main thread can.
• The only difference is that run( ) establishes the entry point for another,
concurrent thread of execution within our program.
• This thread will end when run( ) returns.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
19. class MyThread implements Runnable
{
Thread t;
MyThread()
{
t = new Thread(this, "My Thread");
System.out.println("Child thread: " + t);
t.start();
}
public void run()
{
try {
for(int i = 5; i > 0; i--)
{
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
}
catch (InterruptedException e)
{
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
20. class ThreadDemo1
{
public static void main(String args[])
{
new MyThread();
try {
for(int i = 5; i > 0; i--)
{
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
21. Extending Thread Class
• The second way to create a thread is to create a new class that
extends Thread.
• Create an instance of that class.
• Override the run( ) method.
• Call start( ) to begin execution of the new thread.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
22. class MyThread1 extends Thread {
MyThread1() {
super("Demo Thread");
System.out.println("Child thread: " + this);
start(); }
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(500); }
}
catch (InterruptedException e) {
System.out.println("Child interrupted."); }
System.out.println("Exiting child thread.");
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
23. class ExtendThread {
public static void main(String args[]) {
new MyThread1();
try {
for(int i = 5; i > 0; i--) {
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
}
catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
24. Creating Multiple Threads
class MultiThreadDemo
{
public static void main(String args[]) {
new MyThread2("One");
new MyThread2("Two");
new MyThread2("Three");
try {
Thread.sleep(10000);
}
catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
25. class MyThread2 implements Runnable {
String name; Thread t;
MyThread2(String threadname)
{
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); }
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println(name + "Interrupted");
}
System.out.println(name + " exiting.");
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
26. Notice the call to sleep(10000) in main( ).
• This causes the main thread to sleep for ten seconds and ensures that
it will finish last.
• Two ways exist to determine whether a thread has finished.
• Call isAlive( ) on the thread.
final boolean isAlive( )
• Call join( ) on thread.
final void join( ) throws InterruptedException
• This method waits until the thread on which it is called terminates.
• Additional forms of join( ) allow to specify a maximum amount of
time that you want to wait for the specified thread to terminate.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
27. Synchronization
• Race Condition: Multiple threads calling the same method, on
the same object, at the same time.
• When two or more threads need access to a shared resource,
they need some way to ensure that the resource will be used by
only one thread at a time.
• The process by which this is achieved is called synchronization.
• Monitor (Semaphore) is used for synchronization.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
28. Monitor
• A monitor is an object that is used as a mutually exclusive lock,
or mutex.
• Only one thread can own a monitor at a given time.
• When a thread acquires a lock, it is said to have entered the
monitor.
• All other threads attempting to enter the locked monitor will be
suspended until the first thread exits the monitor.
• These other threads are said to be waiting for the monitor.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
29. Need of Synchronization: Example
class Callme
{
void call(String msg)
{
System.out.print("[" + msg);
try {
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Interrupted");
}
System.out.println("]");
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
30. Example
class Caller implements Runnable
{
String msg;
Callme target;
Thread t;
public Caller(Callme targ, String s)
{
target = targ;
msg = s;
t = new Thread(this);
t.start();
}
public void run()
{
target.call(msg);
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
31. Example
class Synch
{
public static void main(String args[])
{
Callme target = new Callme();
Caller ob1 = new Caller(target, "Hello");
Caller ob2 = new Caller(target, "Synchronized");
Caller ob3 = new Caller(target, "World");
// wait for threads to end
try {
ob1.t.join();
ob2.t.join();
ob3.t.join();
} catch(InterruptedException e)
{
System.out.println("Interrupted");
}
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
32. Using Synchronized Methods
• Methods qualified with synchronized keyword are called
Synchronized Methods.
• While a thread is inside a synchronized method, all other threads
that try to call it (or any other synchronized method) on the same
instance have to wait.
• To exit the monitor and relinquish control of the object to the
next waiting thread, the owner of the monitor simply returns
from the synchronized method.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
33. Using Synchronized Methods
• A synchronized method can be executed by only one thread at a
time.
Example: Synch.java and Synch1.java
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
34. Synchronized Method: Example
class Callme
{
synchronized void call(String msg)
{
System.out.print("[" + msg);
try {
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Interrupted");
}
System.out.println("]");
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
35. The synchronized Statement
• A synchronized block ensures that a call to a method occurs only
after the current thread has successfully entered object’s monitor.
• put calls to the methods defined by the class inside a
synchronized block.
synchronized(object)
{
// statements to be synchronized
}
• Here, object is a reference to the object being synchronized.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
36. Synchronized Statement: Example
class Caller implements Runnable
{
String msg;
Callme target;
Thread t;
public Caller(Callme targ, String s)
{
target = targ;
msg = s;
t = new Thread(this);
t.start();
}
public void run()
{
synchronized(target)
{ // synchronized block
target.call(msg);
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
}
}
37. Suspending, Resuming, and Stopping Threads
• Execution of a thread can be suspended.
• Restarting the execution of a suspended thread is also possible.
• Prior to Java 2, suspend( ) and resume( ) methods (defined by
Thread), were used to pause and restart the execution of a thread.
final void suspend( )
final void resume( )
• The Thread class also defines a method called stop( ) that stops a thread.
final void stop( )
• Once a thread has been stopped, it cannot be restarted using resume( ).
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
38. • suspend( ), resume( ), and stop( ) methods defined by Thread were
deprecated by Java2.
Why???
• suspend( ) can sometimes cause serious system failures as in case
of locking.
• resume() is deprecated because it cannot be used without the
suspend( ).
• stop( ) can also cause serious system failures so it was also
deprecated.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
40. New Approach for suspending, resuming and
stopping a thread
• A thread must be designed so that the run( ) method periodically
checks to determine whether that thread should suspend, resume, or
stop its own execution.
• This is accomplished by establishing a flag variable that indicates the
execution state of the thread.
– As long as this flag is set to “running,” the run( ) method must continue to let
the thread execute.
– If this variable is set to “suspend,” the thread must pause.
– If it is set to “stop,” the thread must terminate.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
41. class NewThread implements Runnable
{
NewThread(String threadname) {}
public void run() {}
void mysuspend() {
suspendFlag = true;
}
synchronized void myresume() {
suspendFlag = false;
notify();
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
42. class SuspendResume
{
public static void main(String args[]) {
NewThread ob1 = new NewThread("One");
NewThread ob2 = new NewThread("Two");
try {
Thread.sleep(1000);
ob1.mysuspend();
System.out.println("Suspending thread One");
Thread.sleep(1000);
ob1.myresume();
System.out.println("Resuming thread One");
ob2.mysuspend();
System.out.println("Suspending thread Two");
Thread.sleep(1000);
ob2.myresume();
System.out.println("Resuming thread Two");
}
catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
43. try {
System.out.println("Waiting for threads to finish.");
ob1.t.join();
ob2.t.join();
}
catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
44. •
wait( ) and notify( ) methods are inherited from Object class can be used to
control the execution of a thread.
•
The NewThread class contains a boolean instance variable named
suspendFlag, which is used to control the execution of the thread.
•
It is initialized to false by the constructor.
•
The run( ) method contains a synchronized statement block that checks
suspendFlag.
•
If that variable is true, the wait( ) method is invoked to suspend the execution
of the thread.
•
The mysuspend( ) method sets suspendFlag to true.
•
The myresume( ) method sets suspendFlag to false and invokes notify( ) to
wake up the thread.
•
Finally, the main( ) method has been modified to invoke the mysuspend( )
and myresume( ) methods.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
45. Inter-thread Communication
• wait( ), notify( ), and notifyAll( ) methods are used for
communication among threads.
• All three methods can be called only from within a synchronized
context.
• wait( ) tells the calling thread to give up the monitor and go to sleep
until some other thread enters the same monitor and calls notify( ).
• notify( ) wakes up a thread that called wait( ) on the same object.
• notifyAll( ) wakes up all the threads that called wait( ) on the same
object. One of the threads will be granted access.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
46. Inter-thread Communication
• These methods are declared within Object, as shown here:
final void wait( ) throws InterruptedException
final void notify( )
final void notifyAll( )
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)