SlideShare a Scribd company logo
1 of 113
Download to read offline
●

Creating a Java Thread

●

Synchronized Keyword

●

Wait and Notify
●

●

●

Java threads are a way to have different parts of your program
running at the same time.

For example, you can create an application that accepts input
from different users at the same time, each user handled by a
thread.
Also, most networking applications involve threads
–

you would need to create a thread that would wait for incoming
messages while the rest of your program handles your outgoing
messages.
●

There are two ways to create a java thread
–

Extending the Thread class

–

Implementing the runnable interface
●

We will create a thread that simply prints out a number 500
times in a row.
class MyThread extends Thread {
int i;
MyThread(int i) {
this.i = i;
}
public void run() {
for (int ctr=0; ctr < 500; ctr++) {
System.out.print(i);
}
}
}
●

To show the difference between parallel and non-parallel
execution, we have the following executable MyThreadDemo
class
class MyThreadDemo {
public static void main(String args[]) {
MyThread t1 = new MyThread(1);
MyThread t2 = new MyThread(2);
MyThread t3 = new MyThread(3);
t1.run();
t2.run();
t3.run();
System.out.print("Main ends");
}}
●

Upon executing MyThreadDemo, we get output that looks like
this
$ java MyThreadDemo
1111111111...22222222.....33333......Main ends

●

●

●

As can be seen, our program first executed t1's run method,
which prints 500 consecutive 1's.
After that t2's run method, which prints consecutuve 2's, and
so on.
Main ends appears at the last part of the output as it is the last
part of the program.
●

What happened was serial execution, no multithreaded
execution occurred
–

This is because we simply called MyThread's run() method
●

To start parallel execution, we call MyThread's start() method,
which is built-in in all thread objects.
class MyThreadDemo {
public static void main(String args[]) {
MyThread t1 = new MyThread(1);
MyThread t2 = new MyThread(2);
MyThread t3 = new MyThread(3);
t1.start();
t2.start();
t3.start();
System.out.print("Main ends");
}}
●

When we run MyThreadDemo we can definitely see that three
run methods are executing at the same time
> java MyThreadDemo
111111111122331122321122333331111Main ends111333222...
●

Note the appearance of the "Main ends" string in the middle of
the output sequence
> java MyThreadDemo
111111111122331122321122333331111Main ends111333222...

●

This indicates that the main method has already finished
executing while thread 1, thread 2 and thread 3 are still
running.
●

Running a main method creates a thread.
–

●

Normally, your program ends when the main thread ends.

However, creating and then running a thread's start method
creates a whole new thread that executes its run() method
independent of the main method.
●

●

Another way to create a thread is to implement the Runnable
interface.

This may be desired if you want your thread to extend another
class.
–

By extending another class, you will be unable to extend class thread
as Java classes can only extend a single class.

–

However, a class can implement more than one interface.
●

The following is our MyThread class created by implementing
the runnable interface.
class MyThread implements Runnable {
... <thread body is mostly the same>
}
●

Classes that implement Runnable are instantiated in a
different way.
–

Thread t1 = new Thread(new MyThread(1));
●

●

Note that implementing an interface requires you to define all
the methods in the interface

For the Runnable interface, you are required in your
implementing class to define the run() method or your
program will not run.
………………

……………………………
Pausing and Joining
Threads
Threads can be paused by the sleep() method.

●

For example, to have MyThread pause for half a second
before it prints the next number, we add the following lines of
code to our for loop.

●

for (int ctr=0; ctr < 500; ctr++) {
System.out.print(i);
try {
Thread.sleep(500); // 500 miliseconds
} catch (InterruptedException e) { }
}

●

Thread.sleep() is a static method of class thread and can
be invoked from any thread, including the main thread.
for (int ctr=0; ctr < 500; ctr++) {
System.out.print(i);
try {
Thread.sleep(500); // 500 miliseconds
} catch (InterruptedException e) { }
}

●

The InterruptedException is an unchecked exeception that is
thrown by code that stops a thread from running.
–

●

Unchecked exception causing lines must be enclosed in a try-catch, in
this case, Thread.sleep().

An InterruptedException is thrown when a thread is waiting,
sleeping, or otherwise paused for a long time and another
thread interrupts it using the interrupt() method in class
Thread.
●

●

You can also make a thread stop until another thread finishes
running by invoking a thread's join() method.

For instance, to make our main thread to stop running until
thread t1 is finished, we could simply say...
public static void main(String args[]) {
...
t1.start();
try {
t1.join();
} catch (InterruptedException e) { }
t2.start();
●

This would cause all the 1's to be printed out before thread
2 and thread 3 start.
………………

……………………………
Critical Section
Problem
●

●

This section we will find out how to implement a solution to the
critical section problem using Java

Recall that only a single process can enter its critical section,
all other processes would have to wait
–

No context switching occurs inside a critical section
●

Instead of printing a continuous stream of numbers, MyThread
calls a print10() method in a class MyPrinter
–

●

print10() prints 10 continuous numbers on a single line before a
newline.

Our goal is to have these 10 continuous numbers printed
without any context switches occuring.
–

Our output should be:
...
1111111111
1111111111
2222222222
3333333333
1111111111
...
●

We define a class MyPrinter that will have our print10()
method

class MyPrinter {
public void print10(int value) {
for (int i = 0; i < 10; i++) {
System.out.print(value);
}
System.out.println(""); // newline after 10 numbers
}
}
●

Instead of printing numbers directly, we use the print10()
method in MyThread, as shown by the following
class MyThread extends Thread {
int i;
MyPrinter p;
MyThread(int i) {
this.i = i;
p = new MyPrinter();
}
public void run() {
for (int ctr=0; ctr < 500; ctr++) {
p.print10(i);
}
}
}
●

First, we will try to see the output of just a single thread
running.
class MyThreadDemo {
public static void main(String args[]) {
MyThread t1 = new MyThread(1);
// MyThread t2 = new MyThread(2);
// MyThread t3 = new MyThread(3);
t1.start();
// t2.start();
// t3.start();
System.out.print("Main ends");
}}

We comment out
the other threads
for now...
●

When we run our MyThreadDemo, we can see that the output
really does match what we want.
> java MyThreadDemo
1111111111
1111111111
1111111111
1111111111
1111111111
...
●

However, let us try to see the output with other threads.

class MyThreadDemo {
public static void main(String args[]) {
MyThread t1 = new MyThread(1);
MyThread t2 = new MyThread(2);
MyThread t3 = new MyThread(3);
t1.start();
t2.start();
t3.start();
System.out.print("Main ends");
}}

We run all three
threads
Our output would look like the following

●

> java MyThreadDemo
1111111111
111112222222
1111
22233333332
...
●

We do not achieve our goal of printing 10 consecutive
numbers in a row
–

all three threads are executing the print10's method at the same
time, thus having more than one number appearing on a single
line.
●

To achieve our goal there should not be any context switches
happening inside print10()
–

●

●

Only a single thread should be allowed to execute inside print10()

As can be seen, this is an example of the critical section
problem
To solve this, we can use some of the techniques discussed in
our synchronization chapter
●
●
●
●

Busy Wait
Wait and Notify

Semaphores
Monitors
●

●

Now we will discuss Java's
solution to the critical
section problem
Java uses a monitor
construct
–

Only a single thread may run
inside the monitor

Even if P2 is calling method2(), it
has to wait for P1 to finish
●

To turn an object into a monitor, simply put the synchronized
keyword on your method definitions
–

Only a single thread can run any synchronized method in an object

class MyPrinter {
public synchronized void print10(int value) {
for (int i = 0; i < 10; i++) {
System.out.print(value);
}
System.out.println(""); // newline after 10
numbers
}
}
However, even if we did turn our print10() method into a
synchronized method, it will still not work

●

> java MyThreadDemo
1111111111
111112222222
1111
22233333332
...

●

To find out how to make it work, we need to first find out
how the synchronized keyword works
●

Every object in Java has an intrinsic lock
●

When a thread tries to run a synchronized method, it first tries
to get a lock on the object
●

If a thread is successful, then it owns the lock and executes
the synchronized code.
●

Other threads cannot run the synchronized code because the
lock is unavailable
●

Threads can only enter once the thread owning the lock
leaves the synchronized method
●

Waiting threads would then compete on who gets to go next
●

So why doesn't our example work?

●

Because each thread has its own copy of the MyPrinter object!

class MyThread extends Thread {
int i;
MyPrinter p;
MyThread(int i) {
this.i = i;
p = new MyPrinter();
// each MyThread creates
its own MyPrinter!
}
public void run() {
for (int ctr=0; ctr < 500; ctr++) {
p.print10(i);
}
}
}
●

Recall our definition of synchronized methods
–

●

Only a single thread can run any synchronized method in an object

Since each thread has its own MyPrinter object, then no
locking occurs
●

So, to make it work, all threads must point to the same
MyPrinter object
●

Note how all MyThreads now have the same MyPrinter object

class MyThread extends Thread {
int i;
MyPrinter p;
MyThread(int i, MyPrinter p) {
this.i = i; this.p = p
}
public synchronized void run() {
for (int ctr=0; ctr < 500;
ctr++) {
p.print10(i);
}
}
}

class MyThreadDemo {
public static void main(String args[]) {
MyPrinter p = new
MyPrinter();
MyThread t1 = new
MyThread(1,p);
MyThread t2 = new
MyThread(2,p);
MyThread t3 = new
MyThread(3,p);
t1.start();
t2.start();
t3.start();
}}
●

A way to visualize it is that all Java objects can be doors
–

A thread tries to see if the door is open

–

Once the thread goes through the door, it locks it behind it,

–

No other threads can enter the door because our first thread has
locked it from the inside

–

Other threads can enter if the thread inside unlocks the door and goes
out

–

Lining up will only occur if everyone only has a single door to the
critical region
●

Running MyThreadDemo now correctly shows synchronized
methods at work
>java MyThreadDemo
1111111111
1111111111
1111111111
2222222222
3333333333
...
●

Only a single thread can run any synchronized method in an
object
–

●

If an object has a synchronized method and a regular method, only one
thread can run the synchronized method while multiple threads can run
the regular method

Threads that you want to have synchronized must share the
same monitor object
●

●

Aside from synchronized methods, Java also allows for
synchronized blocks.

You must specify what object the intrinsic lock comes from
●

Our print10() implementation, done using synchronized
statements, would look like this

class MyPrinter {
public void print10(int value) {
synchronized(this) {
for (int i = 0; i < 10; i++) {
System.out.print(value);
}
System.out.println(""); // newline after
10 numbers
}
}
}
We use a MyPrinter object for the
lock, replicating what the
synchronized method does
●

●

Synchronized blocks allow for flexibility, in the case if we want
our intrinsic lock to come from an object other than the current
object.
For example, we could define MyThread's run method to
perform the lock on the MyPrinter object before calling print10

class MyThread extends Thread {
MyPrinter p;
...
public void run() {
for (int i = 0; i < 100; i++) {
synchronized(p) {
p.print10(value);
}
}}}

Note that any lines
before and after our
synchronized block can
be executed
concurrently by threads
●

Also, the use of the synchronized block allows for more
flexibility by allowing different parts of code to be locked with
different objects.
●

For example, consider a modified MyPrinter which has two
methods, which we'll allow to run concurrently

class MyPrinter {
The blocks inside print10() and
Object lock1 = new Object();
squareMe() can run at the same time
Object lock2 = new Object();
because they use different objects for
public void print10(int value) {
their locking mechanism
synchronized(lock1) {
for (int i = 0; i < 10; i++) {
System.out.print(value);
}
System.out.println(""); // newline after 10 numbers
}
}
Synchronization still applies. For
public int squareMe(int i) {
example only a single thread can
synchronized (lock2) {
run the synchronized block in
return i * i;
squareMe() at any point in time
}
}
}
●

Creating a Java Thread

●

Synchronized Keyword

●

Wait and Notify

●

High Level Cuncurrency Objects
●

●

We will now discuss a solution to the Producer-Consumer
problem.

Instead of using beer, the producer will store in a shared stack
a random integer value which will be retrieved by the
consumer process.
●

As can be seen, we would need to have a place to store our
array of integers and our top variable, which should be shared
among our producer and consumer.
–

MAXCOUNT is the maximum number of items the Producer produces

–

We'll assume the maximum array size of 10000 for now

class SharedVars {
final int MAXCOUNT = 100;
int array[] = new int[10000];
int top;
// <more code to follow>
}
●

●

To keep things object-oriented, we will place the code for
placing values in the stack and getting values in the stack in
our SharedVars class
Our next slide shows our implementation
class SharedVars {
final int MAXCOUNT = 100;
int array[] = new int[10000];
int top;
// method for inserting a value
public void insertValue(int value) {
if (top < array.length) {
array[top] = value;
top++;
}
}
// method for getting a value
public int getValue() {
if (top > 0) {
top--;
return array[top];
}
else
return -1;

}}
●

●

Now we will slowly build our Producer and Consumer class
We can say that our Producer and Consumer should be
implemented as threads.
–

Thus we have our code below, including a class that starts these
threads

class Producer extends Thread {
public void run() {
}
}

class Consumer extends Thread {
public void run() {
}
}

class PCDemo {
public static void main(String args[]) {
Producer p = new Producer();
Consumer c = new Consumer();
p.start();
c.start();
}}
●

Producer and Consumer should have the same SharedVars
object
class Producer extends Thread {
SharedVars sv;
Producer(SharedVars sv) {
this.sv = sv;
}
public void run() {
}
}

class Consumer extends Thread {
SharedVars sv;
Consumer(SharedVars sv) {
this.sv = sv;
}
public void run() {
}
}

class PCDemo {
public static void main(String args[]) {
SharedVars sv = new
SharedVars();
Producer p = new Producer(sv);
Consumer c = new Consumer(sv);
p.start();
c.start();
}}
●

There is no more need to modify our PCDemo class so we will
not show it anymore.
●

We want our producer to produce a random integer and store
it in SharedVars. Thus our Producer code would look like this
class Producer extends Thread {
SharedVars sv;
Producer(SharedVars sv) {
this.sv = sv;
}
public void run() {
int value = (int)(Math.random() * 100); // random value from
0 to 100
sv.insertValue(value);
System.out.println("Producer: Placing value: " + value);
}
}
●

We want our Producer to do this a hundred times, so we will
add a loop to our code
class Producer extends Thread {
SharedVars sv;
Producer(SharedVars sv) {
this.sv = sv;
}
public void run() {
for (int i = 0; i < 100; i++) {
int value = (int)(Math.random() * 100); // random
value from 0 to 100
sv.insertValue(value);
System.out.println("Producer: Placing value: " +
value);
}
}
}
●

Finally, we have our producer pause for a maximum of 5
seconds each time it places a value
class Producer extends Thread {
SharedVars sv;
Producer(SharedVars sv) {
this.sv = sv;
}
public void run() {
for (int i = 0; i < 100; i++) {
int value = (int)(Math.random() * 100); // random
value from 0 to 100
sv.insertValue(value);
System.out.println("Producer: Placing value: " +
value);
try {
Thread.sleep((int)(Math.random() *
5000)); // sleep 5s max

} catch (InterruptedException e) { }
}
}

}
●

Now, we want our consumer to get a value from SharedVars

class Consumer extends Thread {
SharedVars sv;
Consumer(SharedVars sv) {
this.sv = sv;
}
public void run() {
int value = sv.getValue();
System.out.println("Consumer: I got value:" + value);
}
}
●

We want to get a value 100 times

class Consumer extends Thread {
SharedVars sv;
Consumer(SharedVars sv) {
this.sv = sv;
}
public void run() {
for (int i = 0; i < 100; i++) {
int value = sv.getValue();
System.out.println("Consumer: I got value:" +
value);
}
}
}
●

Finally, just like producer, we pause for a maximum of 5
seconds on each loop
class Consumer extends Thread {
SharedVars sv;
Consumer(SharedVars sv) {
this.sv = sv;
}
public void run() {
for (int i = 0; i < 100; i++) {
int value = sv.getValue();
System.out.println("Consumer: I got value:" +
value);

try {
Thread.sleep((int)(Math.random() *
5000)); // sleep 5s max

} catch (InterruptedException e) { }
}
}
}
●

As was discussed in a previous chapter, we try to avoid a race
condition between insertValue() and getValue()

Consumer getValue(): top = top – 1
Producer insertValue(): array[top] = value // this would overwrite an existing value!
Producer insertValue(): top = top + 1
Consumer getValue(): return array[top] // consumer returns an already returned value
●

●

We do not want getValue() and insertValue() to run at the
same time

Therefore, we modify SharedVars to use synchronized blocks
–

We could also use synchronized methods but we need some nonsynchronized commands
class SharedVars {
final int MAXCOUNT = 100;
int array[] = new int[10000];
int top;
public void insertValue(int value) { // method for inserting a value
synchronized(this) {
if (top < array.length) {
array[top] = value;
top++;
}
}
}
public int getValue() { // method for getting a value
synchronized(this) {
if (top > 0) {
top--;
return array[top];
}
else
return -1;
}
}
}
Upon executing our program, the output of our code would
look something like this:

●

Producer inserts value: 15
Consumer got: 15
Consumer got: -1
Producer inserts value: 50
Producer inserts value: 75
Consumer got: 75
...
●

Note that both Consumer and Producer are running at the
same time.
Producer inserts value: 15
Consumer got: 15
Consumer got: -1
Producer inserts value: 50
Producer inserts value: 75
Consumer got: 75
...
●

Notice that, if we try to get a value from the array and there
isn't any, we return a value -1.
–

–
●

You can see this clearly if you increase the Producer delay to 10s.

Producer adds a value every 10s, consumer gets one every 5s

Wouldn't it be better if we have our Consumer wait for the
Producer to produce something instead of just getting -1?
We could implement a busy wait for this

●

public int getValue() {
while (top <= 0) { } // do nothing
synchronized(this) {
top--;
return array[top];
}}
●

Note how we placed this outside our synchronized block
–

Having our busy wait inside would mean blocking out producer
thread from insertValue(), which is what is needed to break the
busy wait
●

●

●

A better solution is to use the wait() method, defined in class
Object, and is therefore inherited by all objects

A thread invoking wait() will suspend the thread
However, a thread invoking wait() must own the intrinsic lock
of the object it is calling wait() from
–

●

If we are going to call this.wait() it has to be in synchronized(this) block

Also, as with all methods that suspend thread execution, like
join() and sleep(), our wait() method must be in a try-catch
block that catches InterruptedExceptions.
// called by Consumer thread
public int getValue() {
synchronized(this) {
if (top <= 0) {
try {
this.wait();
} catch (InterruptedException e) { }
}
top--;
return array[top];

}}
●

●

All threads that call wait() on an object are placed in a pool of
waiting threads for that object.

Execution resumes when another thread calls the notify()
method of the object our first thread is waiting on
–

The notify() method is defined in class Object.

–

All objects have wait() and notify()
●

For our example, our producer thread, after inserting on an
empty array, would notify the consumer thread that it has
placed a value in our array by calling the notify() method on
the SharedVars object
–

Recall that Producer and Consumer both have a reference to the same
SharedVars object.

–

The producer calling notify() from insertValue() would inform any the
consumer waiting on the same SharedVar object.
// called by producer
public void insertValue(int value) {
synchronized(this) {
if (top < array.length) {
array[top] = value;
top++;
if (top == 1) { // we just inserted on an empty array
this.notify(); // notify sleeping thread
}
}
}
}
class SharedVars {
final int MAXCOUNT = 100;
int array[] = new int[10000];
int top;
public void insertValue(int value) {
synchronized(this) {
if (top < array.length) {
array[top] = value;
top++;
if (top == 1) { // we just inserted on an empty array
this.notify(); // notify any sleeping thread
}
}
}
}
public int getValue() {
synchronized(this) {
if (top <= 0) {
try {
this.wait();
} catch (InterruptedException e) { }
}
top--;
return array[top];
}
}
}
●

●

When the notify() method of an object is called, then a single
waiting thread on that object is signaled to get ready to
resume execution.
After our Producer thread exits insertValue and releases the
lock, our Consumer thread gets the lock once again and
resumes its execution.
●

●

Another method called notifyAll() notifies all the waiting
threads.

These waiting threads would then compete to see which
single thread resumes execution, the rest of the threads would
once again wait.
●

●

If you run the demo program again, you can see that, even if
the producer is slow, the consumer waits until the producer
gives out a value before getting that value
Note that you must also consider inserting on a full array
–

Producer must wait until consumer has taken away some values
before inserting

–

We will leave the implementation of this as an exercise
●

Note that this same thing must occur on inserting on a full
array
–

Producer must wait until consumer has taken away some values
before inserting

–

We will leave the implementation of this as an exercise.
●

●

Alice and Bob are at the office. There is a doorway that they have
to pass through every now and then. Since Bob has good manners,
if he gets to the door, he always makes sure that he always opens
the door and waits for Alice to go through before he does. Alice by
herself simply goes through the door.
Your task is to write an Alice and Bob thread that mimics this
behavior. To simulate having to pass through the door, have a delay
in each thread for a random amount of time, maximum of 5
seconds. The output of your code should look something like this:
Alice: I go through the door
Alice: I go through the door

Bob: I get to the door and wait for Alice...
Alice: I go through the door

Bob: I follow Alice
Alice: I go through the door
Bob: I get to the door and wait for Alice

More Related Content

What's hot

What's hot (20)

Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Synchronization.37
Synchronization.37Synchronization.37
Synchronization.37
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
Java Threads
Java ThreadsJava Threads
Java Threads
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java threads
Java threadsJava threads
Java threads
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Java IO Package and Streams
Java IO Package and StreamsJava IO Package and Streams
Java IO Package and Streams
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate ppt
 

Viewers also liked

Learning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and SynchronizationLearning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and Synchronizationcaswenson
 
Java Performance, Threading and Concurrent Data Structures
Java Performance, Threading and Concurrent Data StructuresJava Performance, Threading and Concurrent Data Structures
Java Performance, Threading and Concurrent Data StructuresHitendra Kumar
 
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...Sachintha Gunasena
 
Java Course 10: Threads and Concurrency
Java Course 10: Threads and ConcurrencyJava Course 10: Threads and Concurrency
Java Course 10: Threads and ConcurrencyAnton Keks
 
Applet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java AppletsApplet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java Appletsamitksaha
 
Multithread Programing in Java
Multithread Programing in JavaMultithread Programing in Java
Multithread Programing in JavaM. Raihan
 
Threads And Synchronization in C#
Threads And Synchronization in C#Threads And Synchronization in C#
Threads And Synchronization in C#Rizwan Ali
 
Java layoutmanager
Java layoutmanagerJava layoutmanager
Java layoutmanagerArati Gadgil
 
Learning Java 4 – Swing, SQL, and Security API
Learning Java 4 – Swing, SQL, and Security APILearning Java 4 – Swing, SQL, and Security API
Learning Java 4 – Swing, SQL, and Security APIcaswenson
 
java Unit4 chapter1 applets
java Unit4 chapter1 appletsjava Unit4 chapter1 applets
java Unit4 chapter1 appletsraksharao
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in JavaAyesha Kanwal
 
Java class 3
Java class 3Java class 3
Java class 3Edureka!
 
Java class 6
Java class 6Java class 6
Java class 6Edureka!
 
Concurrency Utilities in Java 8
Concurrency Utilities in Java 8Concurrency Utilities in Java 8
Concurrency Utilities in Java 8Martin Toshev
 
Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.David Gómez García
 

Viewers also liked (20)

Learning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and SynchronizationLearning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and Synchronization
 
Java Performance, Threading and Concurrent Data Structures
Java Performance, Threading and Concurrent Data StructuresJava Performance, Threading and Concurrent Data Structures
Java Performance, Threading and Concurrent Data Structures
 
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
 
Java Course 10: Threads and Concurrency
Java Course 10: Threads and ConcurrencyJava Course 10: Threads and Concurrency
Java Course 10: Threads and Concurrency
 
Applet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java AppletsApplet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java Applets
 
Multithread Programing in Java
Multithread Programing in JavaMultithread Programing in Java
Multithread Programing in Java
 
Threads And Synchronization in C#
Threads And Synchronization in C#Threads And Synchronization in C#
Threads And Synchronization in C#
 
Java layoutmanager
Java layoutmanagerJava layoutmanager
Java layoutmanager
 
Learning Java 4 – Swing, SQL, and Security API
Learning Java 4 – Swing, SQL, and Security APILearning Java 4 – Swing, SQL, and Security API
Learning Java 4 – Swing, SQL, and Security API
 
java Unit4 chapter1 applets
java Unit4 chapter1 appletsjava Unit4 chapter1 applets
java Unit4 chapter1 applets
 
Java 8 parallel stream
Java 8 parallel streamJava 8 parallel stream
Java 8 parallel stream
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in Java
 
Java thread
Java threadJava thread
Java thread
 
Thread priorities
Thread prioritiesThread priorities
Thread priorities
 
Java class 3
Java class 3Java class 3
Java class 3
 
Java class 6
Java class 6Java class 6
Java class 6
 
Concurrency Utilities in Java 8
Concurrency Utilities in Java 8Concurrency Utilities in Java 8
Concurrency Utilities in Java 8
 
Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.
 
Java
JavaJava
Java
 
Applets in java
Applets in javaApplets in java
Applets in java
 

Similar to Java Thread Synchronization

Chap3 multi threaded programming
Chap3 multi threaded programmingChap3 multi threaded programming
Chap3 multi threaded programmingraksharao
 
java-thread
java-threadjava-thread
java-threadbabu4b4u
 
Python multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugamPython multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugamNavaneethan Naveen
 
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxArulmozhivarman8
 
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in JavaJayant Dalvi
 
Session 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptSession 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptTabassumMaktum
 
Session 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptSession 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptTabassumMaktum
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadKartik Dube
 
Java Multithreading.pptx
Java Multithreading.pptxJava Multithreading.pptx
Java Multithreading.pptxRanjithaM32
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptxssuserfcae42
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in javaElizabeth alexander
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34myrajendra
 

Similar to Java Thread Synchronization (20)

Chap3 multi threaded programming
Chap3 multi threaded programmingChap3 multi threaded programming
Chap3 multi threaded programming
 
advanced java ppt
advanced java pptadvanced java ppt
advanced java ppt
 
Intake 38 12
Intake 38 12Intake 38 12
Intake 38 12
 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
 
Threading
ThreadingThreading
Threading
 
Threads in python
Threads in pythonThreads in python
Threads in python
 
Java unit 12
Java unit 12Java unit 12
Java unit 12
 
java-thread
java-threadjava-thread
java-thread
 
Python multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugamPython multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugam
 
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptx
 
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in Java
 
Session 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptSession 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.ppt
 
Session 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptSession 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.ppt
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
 
Java Multithreading.pptx
Java Multithreading.pptxJava Multithreading.pptx
Java Multithreading.pptx
 
javathreads
javathreadsjavathreads
javathreads
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34
 

Recently uploaded

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 

Recently uploaded (20)

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 

Java Thread Synchronization

  • 1.
  • 2. ● Creating a Java Thread ● Synchronized Keyword ● Wait and Notify
  • 3. ● ● ● Java threads are a way to have different parts of your program running at the same time. For example, you can create an application that accepts input from different users at the same time, each user handled by a thread. Also, most networking applications involve threads – you would need to create a thread that would wait for incoming messages while the rest of your program handles your outgoing messages.
  • 4. ● There are two ways to create a java thread – Extending the Thread class – Implementing the runnable interface
  • 5. ● We will create a thread that simply prints out a number 500 times in a row. class MyThread extends Thread { int i; MyThread(int i) { this.i = i; } public void run() { for (int ctr=0; ctr < 500; ctr++) { System.out.print(i); } } }
  • 6. ● To show the difference between parallel and non-parallel execution, we have the following executable MyThreadDemo class class MyThreadDemo { public static void main(String args[]) { MyThread t1 = new MyThread(1); MyThread t2 = new MyThread(2); MyThread t3 = new MyThread(3); t1.run(); t2.run(); t3.run(); System.out.print("Main ends"); }}
  • 7. ● Upon executing MyThreadDemo, we get output that looks like this $ java MyThreadDemo 1111111111...22222222.....33333......Main ends ● ● ● As can be seen, our program first executed t1's run method, which prints 500 consecutive 1's. After that t2's run method, which prints consecutuve 2's, and so on. Main ends appears at the last part of the output as it is the last part of the program.
  • 8. ● What happened was serial execution, no multithreaded execution occurred – This is because we simply called MyThread's run() method
  • 9.
  • 10. ● To start parallel execution, we call MyThread's start() method, which is built-in in all thread objects. class MyThreadDemo { public static void main(String args[]) { MyThread t1 = new MyThread(1); MyThread t2 = new MyThread(2); MyThread t3 = new MyThread(3); t1.start(); t2.start(); t3.start(); System.out.print("Main ends"); }}
  • 11. ● When we run MyThreadDemo we can definitely see that three run methods are executing at the same time > java MyThreadDemo 111111111122331122321122333331111Main ends111333222...
  • 12.
  • 13. ● Note the appearance of the "Main ends" string in the middle of the output sequence > java MyThreadDemo 111111111122331122321122333331111Main ends111333222... ● This indicates that the main method has already finished executing while thread 1, thread 2 and thread 3 are still running.
  • 14. ● Running a main method creates a thread. – ● Normally, your program ends when the main thread ends. However, creating and then running a thread's start method creates a whole new thread that executes its run() method independent of the main method.
  • 15. ● ● Another way to create a thread is to implement the Runnable interface. This may be desired if you want your thread to extend another class. – By extending another class, you will be unable to extend class thread as Java classes can only extend a single class. – However, a class can implement more than one interface.
  • 16. ● The following is our MyThread class created by implementing the runnable interface. class MyThread implements Runnable { ... <thread body is mostly the same> } ● Classes that implement Runnable are instantiated in a different way. – Thread t1 = new Thread(new MyThread(1));
  • 17. ● ● Note that implementing an interface requires you to define all the methods in the interface For the Runnable interface, you are required in your implementing class to define the run() method or your program will not run.
  • 19. Threads can be paused by the sleep() method. ● For example, to have MyThread pause for half a second before it prints the next number, we add the following lines of code to our for loop. ● for (int ctr=0; ctr < 500; ctr++) { System.out.print(i); try { Thread.sleep(500); // 500 miliseconds } catch (InterruptedException e) { } } ● Thread.sleep() is a static method of class thread and can be invoked from any thread, including the main thread.
  • 20. for (int ctr=0; ctr < 500; ctr++) { System.out.print(i); try { Thread.sleep(500); // 500 miliseconds } catch (InterruptedException e) { } } ● The InterruptedException is an unchecked exeception that is thrown by code that stops a thread from running. – ● Unchecked exception causing lines must be enclosed in a try-catch, in this case, Thread.sleep(). An InterruptedException is thrown when a thread is waiting, sleeping, or otherwise paused for a long time and another thread interrupts it using the interrupt() method in class Thread.
  • 21. ● ● You can also make a thread stop until another thread finishes running by invoking a thread's join() method. For instance, to make our main thread to stop running until thread t1 is finished, we could simply say... public static void main(String args[]) { ... t1.start(); try { t1.join(); } catch (InterruptedException e) { } t2.start(); ● This would cause all the 1's to be printed out before thread 2 and thread 3 start.
  • 23. ● ● This section we will find out how to implement a solution to the critical section problem using Java Recall that only a single process can enter its critical section, all other processes would have to wait – No context switching occurs inside a critical section
  • 24. ● Instead of printing a continuous stream of numbers, MyThread calls a print10() method in a class MyPrinter – ● print10() prints 10 continuous numbers on a single line before a newline. Our goal is to have these 10 continuous numbers printed without any context switches occuring. – Our output should be: ... 1111111111 1111111111 2222222222 3333333333 1111111111 ...
  • 25. ● We define a class MyPrinter that will have our print10() method class MyPrinter { public void print10(int value) { for (int i = 0; i < 10; i++) { System.out.print(value); } System.out.println(""); // newline after 10 numbers } }
  • 26. ● Instead of printing numbers directly, we use the print10() method in MyThread, as shown by the following class MyThread extends Thread { int i; MyPrinter p; MyThread(int i) { this.i = i; p = new MyPrinter(); } public void run() { for (int ctr=0; ctr < 500; ctr++) { p.print10(i); } } }
  • 27. ● First, we will try to see the output of just a single thread running. class MyThreadDemo { public static void main(String args[]) { MyThread t1 = new MyThread(1); // MyThread t2 = new MyThread(2); // MyThread t3 = new MyThread(3); t1.start(); // t2.start(); // t3.start(); System.out.print("Main ends"); }} We comment out the other threads for now...
  • 28. ● When we run our MyThreadDemo, we can see that the output really does match what we want. > java MyThreadDemo 1111111111 1111111111 1111111111 1111111111 1111111111 ...
  • 29. ● However, let us try to see the output with other threads. class MyThreadDemo { public static void main(String args[]) { MyThread t1 = new MyThread(1); MyThread t2 = new MyThread(2); MyThread t3 = new MyThread(3); t1.start(); t2.start(); t3.start(); System.out.print("Main ends"); }} We run all three threads
  • 30. Our output would look like the following ● > java MyThreadDemo 1111111111 111112222222 1111 22233333332 ... ● We do not achieve our goal of printing 10 consecutive numbers in a row – all three threads are executing the print10's method at the same time, thus having more than one number appearing on a single line.
  • 31. ● To achieve our goal there should not be any context switches happening inside print10() – ● ● Only a single thread should be allowed to execute inside print10() As can be seen, this is an example of the critical section problem To solve this, we can use some of the techniques discussed in our synchronization chapter
  • 32. ● ● ● ● Busy Wait Wait and Notify Semaphores Monitors
  • 33. ● ● Now we will discuss Java's solution to the critical section problem Java uses a monitor construct – Only a single thread may run inside the monitor Even if P2 is calling method2(), it has to wait for P1 to finish
  • 34. ● To turn an object into a monitor, simply put the synchronized keyword on your method definitions – Only a single thread can run any synchronized method in an object class MyPrinter { public synchronized void print10(int value) { for (int i = 0; i < 10; i++) { System.out.print(value); } System.out.println(""); // newline after 10 numbers } }
  • 35. However, even if we did turn our print10() method into a synchronized method, it will still not work ● > java MyThreadDemo 1111111111 111112222222 1111 22233333332 ... ● To find out how to make it work, we need to first find out how the synchronized keyword works
  • 36. ● Every object in Java has an intrinsic lock
  • 37. ● When a thread tries to run a synchronized method, it first tries to get a lock on the object
  • 38. ● If a thread is successful, then it owns the lock and executes the synchronized code.
  • 39. ● Other threads cannot run the synchronized code because the lock is unavailable
  • 40. ● Threads can only enter once the thread owning the lock leaves the synchronized method
  • 41. ● Waiting threads would then compete on who gets to go next
  • 42. ● So why doesn't our example work? ● Because each thread has its own copy of the MyPrinter object! class MyThread extends Thread { int i; MyPrinter p; MyThread(int i) { this.i = i; p = new MyPrinter(); // each MyThread creates its own MyPrinter! } public void run() { for (int ctr=0; ctr < 500; ctr++) { p.print10(i); } } }
  • 43.
  • 44. ● Recall our definition of synchronized methods – ● Only a single thread can run any synchronized method in an object Since each thread has its own MyPrinter object, then no locking occurs
  • 45. ● So, to make it work, all threads must point to the same MyPrinter object
  • 46. ● Note how all MyThreads now have the same MyPrinter object class MyThread extends Thread { int i; MyPrinter p; MyThread(int i, MyPrinter p) { this.i = i; this.p = p } public synchronized void run() { for (int ctr=0; ctr < 500; ctr++) { p.print10(i); } } } class MyThreadDemo { public static void main(String args[]) { MyPrinter p = new MyPrinter(); MyThread t1 = new MyThread(1,p); MyThread t2 = new MyThread(2,p); MyThread t3 = new MyThread(3,p); t1.start(); t2.start(); t3.start(); }}
  • 47.
  • 48. ● A way to visualize it is that all Java objects can be doors – A thread tries to see if the door is open – Once the thread goes through the door, it locks it behind it, – No other threads can enter the door because our first thread has locked it from the inside – Other threads can enter if the thread inside unlocks the door and goes out – Lining up will only occur if everyone only has a single door to the critical region
  • 49. ● Running MyThreadDemo now correctly shows synchronized methods at work >java MyThreadDemo 1111111111 1111111111 1111111111 2222222222 3333333333 ...
  • 50. ● Only a single thread can run any synchronized method in an object – ● If an object has a synchronized method and a regular method, only one thread can run the synchronized method while multiple threads can run the regular method Threads that you want to have synchronized must share the same monitor object
  • 51. ● ● Aside from synchronized methods, Java also allows for synchronized blocks. You must specify what object the intrinsic lock comes from
  • 52. ● Our print10() implementation, done using synchronized statements, would look like this class MyPrinter { public void print10(int value) { synchronized(this) { for (int i = 0; i < 10; i++) { System.out.print(value); } System.out.println(""); // newline after 10 numbers } } } We use a MyPrinter object for the lock, replicating what the synchronized method does
  • 53. ● ● Synchronized blocks allow for flexibility, in the case if we want our intrinsic lock to come from an object other than the current object. For example, we could define MyThread's run method to perform the lock on the MyPrinter object before calling print10 class MyThread extends Thread { MyPrinter p; ... public void run() { for (int i = 0; i < 100; i++) { synchronized(p) { p.print10(value); } }}} Note that any lines before and after our synchronized block can be executed concurrently by threads
  • 54. ● Also, the use of the synchronized block allows for more flexibility by allowing different parts of code to be locked with different objects.
  • 55. ● For example, consider a modified MyPrinter which has two methods, which we'll allow to run concurrently class MyPrinter { The blocks inside print10() and Object lock1 = new Object(); squareMe() can run at the same time Object lock2 = new Object(); because they use different objects for public void print10(int value) { their locking mechanism synchronized(lock1) { for (int i = 0; i < 10; i++) { System.out.print(value); } System.out.println(""); // newline after 10 numbers } } Synchronization still applies. For public int squareMe(int i) { example only a single thread can synchronized (lock2) { run the synchronized block in return i * i; squareMe() at any point in time } } }
  • 56.
  • 57. ● Creating a Java Thread ● Synchronized Keyword ● Wait and Notify ● High Level Cuncurrency Objects
  • 58. ● ● We will now discuss a solution to the Producer-Consumer problem. Instead of using beer, the producer will store in a shared stack a random integer value which will be retrieved by the consumer process.
  • 59.
  • 60.
  • 61. ● As can be seen, we would need to have a place to store our array of integers and our top variable, which should be shared among our producer and consumer. – MAXCOUNT is the maximum number of items the Producer produces – We'll assume the maximum array size of 10000 for now class SharedVars { final int MAXCOUNT = 100; int array[] = new int[10000]; int top; // <more code to follow> }
  • 62. ● ● To keep things object-oriented, we will place the code for placing values in the stack and getting values in the stack in our SharedVars class Our next slide shows our implementation
  • 63. class SharedVars { final int MAXCOUNT = 100; int array[] = new int[10000]; int top; // method for inserting a value public void insertValue(int value) { if (top < array.length) { array[top] = value; top++; } } // method for getting a value public int getValue() { if (top > 0) { top--; return array[top]; } else return -1; }}
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75. ● ● Now we will slowly build our Producer and Consumer class We can say that our Producer and Consumer should be implemented as threads. – Thus we have our code below, including a class that starts these threads class Producer extends Thread { public void run() { } } class Consumer extends Thread { public void run() { } } class PCDemo { public static void main(String args[]) { Producer p = new Producer(); Consumer c = new Consumer(); p.start(); c.start(); }}
  • 76. ● Producer and Consumer should have the same SharedVars object class Producer extends Thread { SharedVars sv; Producer(SharedVars sv) { this.sv = sv; } public void run() { } } class Consumer extends Thread { SharedVars sv; Consumer(SharedVars sv) { this.sv = sv; } public void run() { } } class PCDemo { public static void main(String args[]) { SharedVars sv = new SharedVars(); Producer p = new Producer(sv); Consumer c = new Consumer(sv); p.start(); c.start(); }}
  • 77. ● There is no more need to modify our PCDemo class so we will not show it anymore.
  • 78. ● We want our producer to produce a random integer and store it in SharedVars. Thus our Producer code would look like this class Producer extends Thread { SharedVars sv; Producer(SharedVars sv) { this.sv = sv; } public void run() { int value = (int)(Math.random() * 100); // random value from 0 to 100 sv.insertValue(value); System.out.println("Producer: Placing value: " + value); } }
  • 79. ● We want our Producer to do this a hundred times, so we will add a loop to our code class Producer extends Thread { SharedVars sv; Producer(SharedVars sv) { this.sv = sv; } public void run() { for (int i = 0; i < 100; i++) { int value = (int)(Math.random() * 100); // random value from 0 to 100 sv.insertValue(value); System.out.println("Producer: Placing value: " + value); } } }
  • 80. ● Finally, we have our producer pause for a maximum of 5 seconds each time it places a value class Producer extends Thread { SharedVars sv; Producer(SharedVars sv) { this.sv = sv; } public void run() { for (int i = 0; i < 100; i++) { int value = (int)(Math.random() * 100); // random value from 0 to 100 sv.insertValue(value); System.out.println("Producer: Placing value: " + value); try { Thread.sleep((int)(Math.random() * 5000)); // sleep 5s max } catch (InterruptedException e) { } } } }
  • 81. ● Now, we want our consumer to get a value from SharedVars class Consumer extends Thread { SharedVars sv; Consumer(SharedVars sv) { this.sv = sv; } public void run() { int value = sv.getValue(); System.out.println("Consumer: I got value:" + value); } }
  • 82. ● We want to get a value 100 times class Consumer extends Thread { SharedVars sv; Consumer(SharedVars sv) { this.sv = sv; } public void run() { for (int i = 0; i < 100; i++) { int value = sv.getValue(); System.out.println("Consumer: I got value:" + value); } } }
  • 83. ● Finally, just like producer, we pause for a maximum of 5 seconds on each loop class Consumer extends Thread { SharedVars sv; Consumer(SharedVars sv) { this.sv = sv; } public void run() { for (int i = 0; i < 100; i++) { int value = sv.getValue(); System.out.println("Consumer: I got value:" + value); try { Thread.sleep((int)(Math.random() * 5000)); // sleep 5s max } catch (InterruptedException e) { } } } }
  • 84. ● As was discussed in a previous chapter, we try to avoid a race condition between insertValue() and getValue() Consumer getValue(): top = top – 1 Producer insertValue(): array[top] = value // this would overwrite an existing value! Producer insertValue(): top = top + 1 Consumer getValue(): return array[top] // consumer returns an already returned value
  • 85.
  • 86. ● ● We do not want getValue() and insertValue() to run at the same time Therefore, we modify SharedVars to use synchronized blocks – We could also use synchronized methods but we need some nonsynchronized commands
  • 87. class SharedVars { final int MAXCOUNT = 100; int array[] = new int[10000]; int top; public void insertValue(int value) { // method for inserting a value synchronized(this) { if (top < array.length) { array[top] = value; top++; } } } public int getValue() { // method for getting a value synchronized(this) { if (top > 0) { top--; return array[top]; } else return -1; } } }
  • 88.
  • 89.
  • 90. Upon executing our program, the output of our code would look something like this: ● Producer inserts value: 15 Consumer got: 15 Consumer got: -1 Producer inserts value: 50 Producer inserts value: 75 Consumer got: 75 ... ● Note that both Consumer and Producer are running at the same time.
  • 91. Producer inserts value: 15 Consumer got: 15 Consumer got: -1 Producer inserts value: 50 Producer inserts value: 75 Consumer got: 75 ... ● Notice that, if we try to get a value from the array and there isn't any, we return a value -1. – – ● You can see this clearly if you increase the Producer delay to 10s. Producer adds a value every 10s, consumer gets one every 5s Wouldn't it be better if we have our Consumer wait for the Producer to produce something instead of just getting -1?
  • 92. We could implement a busy wait for this ● public int getValue() { while (top <= 0) { } // do nothing synchronized(this) { top--; return array[top]; }} ● Note how we placed this outside our synchronized block – Having our busy wait inside would mean blocking out producer thread from insertValue(), which is what is needed to break the busy wait
  • 93. ● ● ● A better solution is to use the wait() method, defined in class Object, and is therefore inherited by all objects A thread invoking wait() will suspend the thread However, a thread invoking wait() must own the intrinsic lock of the object it is calling wait() from – ● If we are going to call this.wait() it has to be in synchronized(this) block Also, as with all methods that suspend thread execution, like join() and sleep(), our wait() method must be in a try-catch block that catches InterruptedExceptions.
  • 94. // called by Consumer thread public int getValue() { synchronized(this) { if (top <= 0) { try { this.wait(); } catch (InterruptedException e) { } } top--; return array[top]; }}
  • 95. ● ● All threads that call wait() on an object are placed in a pool of waiting threads for that object. Execution resumes when another thread calls the notify() method of the object our first thread is waiting on – The notify() method is defined in class Object. – All objects have wait() and notify()
  • 96. ● For our example, our producer thread, after inserting on an empty array, would notify the consumer thread that it has placed a value in our array by calling the notify() method on the SharedVars object – Recall that Producer and Consumer both have a reference to the same SharedVars object. – The producer calling notify() from insertValue() would inform any the consumer waiting on the same SharedVar object.
  • 97. // called by producer public void insertValue(int value) { synchronized(this) { if (top < array.length) { array[top] = value; top++; if (top == 1) { // we just inserted on an empty array this.notify(); // notify sleeping thread } } } }
  • 98. class SharedVars { final int MAXCOUNT = 100; int array[] = new int[10000]; int top; public void insertValue(int value) { synchronized(this) { if (top < array.length) { array[top] = value; top++; if (top == 1) { // we just inserted on an empty array this.notify(); // notify any sleeping thread } } } } public int getValue() { synchronized(this) { if (top <= 0) { try { this.wait(); } catch (InterruptedException e) { } } top--; return array[top]; } } }
  • 99. ● ● When the notify() method of an object is called, then a single waiting thread on that object is signaled to get ready to resume execution. After our Producer thread exits insertValue and releases the lock, our Consumer thread gets the lock once again and resumes its execution.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109. ● ● Another method called notifyAll() notifies all the waiting threads. These waiting threads would then compete to see which single thread resumes execution, the rest of the threads would once again wait.
  • 110. ● ● If you run the demo program again, you can see that, even if the producer is slow, the consumer waits until the producer gives out a value before getting that value Note that you must also consider inserting on a full array – Producer must wait until consumer has taken away some values before inserting – We will leave the implementation of this as an exercise
  • 111. ● Note that this same thing must occur on inserting on a full array – Producer must wait until consumer has taken away some values before inserting – We will leave the implementation of this as an exercise.
  • 112.
  • 113. ● ● Alice and Bob are at the office. There is a doorway that they have to pass through every now and then. Since Bob has good manners, if he gets to the door, he always makes sure that he always opens the door and waits for Alice to go through before he does. Alice by herself simply goes through the door. Your task is to write an Alice and Bob thread that mimics this behavior. To simulate having to pass through the door, have a delay in each thread for a random amount of time, maximum of 5 seconds. The output of your code should look something like this: Alice: I go through the door Alice: I go through the door Bob: I get to the door and wait for Alice... Alice: I go through the door Bob: I follow Alice Alice: I go through the door Bob: I get to the door and wait for Alice