Documente Academic
Documente Profesional
Documente Cultură
Verónica Gaspes
www2.hh.se/staff/vero
Threads in Java
class A{
...
public static void main(String[] cmndLn){
...
new Thread(new T1()).start();
new T2().start();
...
}
}
Threads in Java
class A{
...
public static void main(String[] cmndLn){
...
new Thread(new T1()).start();
new T2().start();
...
}
}
Threads in Java
class A{
...
public static void main(String[] cmndLn){
...
new Thread(new T1()).start();
new T2().start();
...
}
}
Threads in Java
class A{
...
public static void main(String[] cmndLn){
...
new Thread(new T1()).start();
new T2().start();
...
}
}
private int x = 0;
private int x = 0;
import java.util.concurrency.locks.*;
class Counter{
private final Lock lock = new ReentrantLock();
private int x = 0;
public void inc(){
lock.lock();
try{
int tmp = x;
tmp++;
x = tmp;
}finally{lock.unlock();}
}
Threads in Java Monitors Remote Method Invocation
The threads
Example
class CounterUser extends Thread{
Example
In the bounded buffer that is shared by producer threads and
consumer threads:
Producers might have to wait until the buffer is not full.
Consumers might have to wait until the buffer is not empty.
Example
In the bounded buffer that is shared by producer threads and
consumer threads:
Producers might have to wait until the buffer is not full.
Consumers might have to wait until the buffer is not empty.
Example
In the bounded buffer that is shared by producer threads and
consumer threads:
Producers might have to wait until the buffer is not full.
Consumers might have to wait until the buffer is not empty.
Example
In the bounded buffer that is shared by producer threads and
consumer threads:
Producers might have to wait until the buffer is not full.
Consumers might have to wait until the buffer is not empty.
Example
In the bounded buffer that is shared by producer threads and
consumer threads:
Producers might have to wait until the buffer is not full.
Consumers might have to wait until the buffer is not empty.
Used to
Delay a process that cannot safely continue executing until
the monitor’s state satisfies some property.
Awaken a delayed process when the property holds.
How?
1 Inside a monitor declare a variable of the type for condition
variables cond cv .
2 A process blocks on a condition variable by executing
wait(cv) .
3 Processes that are blocked on condition variables get awaken
by signal(cv) .
Threads in Java Monitors Remote Method Invocation
Used to
Delay a process that cannot safely continue executing until
the monitor’s state satisfies some property.
Awaken a delayed process when the property holds.
How?
1 Inside a monitor declare a variable of the type for condition
variables cond cv .
2 A process blocks on a condition variable by executing
wait(cv) .
3 Processes that are blocked on condition variables get awaken
by signal(cv) .
Threads in Java Monitors Remote Method Invocation
Used to
Delay a process that cannot safely continue executing until
the monitor’s state satisfies some property.
Awaken a delayed process when the property holds.
How?
1 Inside a monitor declare a variable of the type for condition
variables cond cv .
2 A process blocks on a condition variable by executing
wait(cv) .
3 Processes that are blocked on condition variables get awaken
by signal(cv) .
Threads in Java Monitors Remote Method Invocation
Used to
Delay a process that cannot safely continue executing until
the monitor’s state satisfies some property.
Awaken a delayed process when the property holds.
How?
1 Inside a monitor declare a variable of the type for condition
variables cond cv .
2 A process blocks on a condition variable by executing
wait(cv) .
3 Processes that are blocked on condition variables get awaken
by signal(cv) .
Threads in Java Monitors Remote Method Invocation
Used to
Delay a process that cannot safely continue executing until
the monitor’s state satisfies some property.
Awaken a delayed process when the property holds.
How?
1 Inside a monitor declare a variable of the type for condition
variables cond cv .
2 A process blocks on a condition variable by executing
wait(cv) .
3 Processes that are blocked on condition variables get awaken
by signal(cv) .
Threads in Java Monitors Remote Method Invocation
Used to
Delay a process that cannot safely continue executing until
the monitor’s state satisfies some property.
Awaken a delayed process when the property holds.
How?
1 Inside a monitor declare a variable of the type for condition
variables cond cv .
2 A process blocks on a condition variable by executing
wait(cv) .
3 Processes that are blocked on condition variables get awaken
by signal(cv) .
Threads in Java Monitors Remote Method Invocation
Used to
Delay a process that cannot safely continue executing until
the monitor’s state satisfies some property.
Awaken a delayed process when the property holds.
How?
1 Inside a monitor declare a variable of the type for condition
variables cond cv .
2 A process blocks on a condition variable by executing
wait(cv) .
3 Processes that are blocked on condition variables get awaken
by signal(cv) .
Threads in Java Monitors Remote Method Invocation
Semantics
Values
The value of a condition variable is a queue of delayed processes.
Wait
The process that executes wait(cv) gets delayed at the rear of
the queue for cv. It relinquishes exclusive access to the monitor.
Signal
Execution of signal(cv) examines cv’s queue. If it is empty it
has no effect. If there are delayed porcesses, it awakens the process
at the front of the queue.
Threads in Java Monitors Remote Method Invocation
Semantics
Values
The value of a condition variable is a queue of delayed processes.
Wait
The process that executes wait(cv) gets delayed at the rear of
the queue for cv. It relinquishes exclusive access to the monitor.
Signal
Execution of signal(cv) examines cv’s queue. If it is empty it
has no effect. If there are delayed porcesses, it awakens the process
at the front of the queue.
Threads in Java Monitors Remote Method Invocation
Semantics
Values
The value of a condition variable is a queue of delayed processes.
Wait
The process that executes wait(cv) gets delayed at the rear of
the queue for cv. It relinquishes exclusive access to the monitor.
Signal
Execution of signal(cv) examines cv’s queue. If it is empty it
has no effect. If there are delayed porcesses, it awakens the process
at the front of the queue.
Threads in Java Monitors Remote Method Invocation
Semantics
Values
The value of a condition variable is a queue of delayed processes.
Wait
The process that executes wait(cv) gets delayed at the rear of
the queue for cv. It relinquishes exclusive access to the monitor.
Signal
Execution of signal(cv) examines cv’s queue. If it is empty it
has no effect. If there are delayed porcesses, it awakens the process
at the front of the queue.
Threads in Java Monitors Remote Method Invocation
java.util.concurrent.locks
java.util.concurrent.locks
java.util.concurrent.locks
java.util.concurrent.locks
java.util.concurrent.locks
Example
class BoundedBuffer<T>{
private Lock lock = new ReentrantLock();
private Condition notFull = lock.newCondition();
private Condition notEmpty = lock.newCondition();
private int count = 0;
private int n;
private T[] buf;
private int front = 0;
private int rear = 0;
...}
Threads in Java Monitors Remote Method Invocation
java.util.concurrent.locks
Example
class BoundedBuffer<T>{
private Lock lock = new ReentrantLock();
private Condition notFull = lock.newCondition();
private Condition notEmpty = lock.newCondition();
private int count = 0;
private int n;
private T[] buf;
private int front = 0;
private int rear = 0;
...}
Threads in Java Monitors Remote Method Invocation
Using Condition
Example
public void deposit(T item){
lock.lock();
try{
while(count == n) notFull.await();
buf[rear] = item;
rear = inc(rear);
count++;
notEmpty.signal();
}catch(InterruptedException e){
System.out.println(e.getMessage());}
finally{ lock.unlock(); }
}
Threads in Java Monitors Remote Method Invocation
Using Condition
Example
public T fetch(){
lock.lock();
try{
while(count == 0) notEmpty.await();
T result = buf[front];
front = inc(front);
count--;
notFull.signal();
return result;
}catch(InterruptedException e){
System.out.println(e.getMessage());return null;}
finally{ lock.unlock(); }
}
Threads in Java Monitors Remote Method Invocation
import java.util.concurrent.locks.*;
class RWScheduler{
public void requestRead()
public void requestWrite()
public void releaseRead()
public void releaseWrite()
}
Threads in Java Monitors Remote Method Invocation
The scheduler has to delay readers and/or writers until they have
the right to use the database. For doing so, it counts the number
of readers and number of writers using the db.
Scheduler code
import java.util.concurrent.locks.*;
class RWScheduler{
private int nr = 0;
private int nw = 0;
private int dr = 0;
private int dw = 0;
import java.util.concurrent.locks.*;
class RWScheduler{
import java.util.concurrent.locks.*;
class RWScheduler{
import java.util.concurrent.locks.*;
class RWScheduler{
import java.util.concurrent.locks.*;
class RWScheduler{
Monitors
We said that monitors are abstract data types with support for
1 mutual exclusion
2 waiting on conditions
Monitors
We said that monitors are abstract data types with support for
1 mutual exclusion
2 waiting on conditions
Monitors
We said that monitors are abstract data types with support for
1 mutual exclusion
2 waiting on conditions
Example
class BoundedBuffer<T>{
private int n;
private T[] buf;
private int front = 0;
private int rear = 0;
/* constructor */
public BoundedBuffer(int size)
/* operations */
public void deposit(T item){
buf[rear] = item;
rear = inc(rear);
}
public T fetch(){...}
}
Threads in Java Monitors Remote Method Invocation
Example
public static void main(String[] args){
BoundedBuffer<Integer> theBuffer =
new BoundedBuffer<Integer>(5);
for(int i = 1;i<10;i++){
new Producer(theBuffer, i).start();
}
for(int i = 1;i<5;i++){
new Consumer(theBuffer, i).start();
}
}
Threads in Java Monitors Remote Method Invocation
Example
class Producer extends Thread{
private BoundedBuffer<Integer> bb;
private int id;
private Random random = new Random();
public Producer(BoundedBuffer<Integer> bb, int id){
this.bb = bb;this.id = id;
}
public void run(){
int x;
while(true){
nap(random.nextInt(5000));
x = random.nextInt(100);
bb.deposit(x);
}
Threads in Java Monitors Remote Method Invocation
An airport display
Example
A client process running on the
computer of the airline operator
can
Example
A display showing the arrival or add an info line by calling
departure times of aircraft at an addRow
airport. It can be used by all the remove an info line by calling
airlines to publish info about their deleteRow
flights. Multiple clients might call
addRow and deleteRow at
the same time
Several processes will serve
these calls concurrently!
Threads in Java Monitors Remote Method Invocation
An airport display
Example
Example A client process running on the
A display showing the arrival or computer of the airline operator
departure times of aircraft at an can
airport. It can be used by all the
add an info line by calling
airlines to publish info about their
addRow
flights.
remove an info line by calling
deleteRow
Multiple clients might call
addRow and deleteRow at
the same time
Several processes will serve
these calls concurrently!
Threads in Java Monitors Remote Method Invocation
An airport display
Example
Example A client process running on the
A display showing the arrival or computer of the airline operator
departure times of aircraft at an can
airport. It can be used by all the
add an info line by calling
airlines to publish info about their
addRow
flights.
remove an info line by calling
deleteRow
Multiple clients might call
addRow and deleteRow at
the same time
Several processes will serve
these calls concurrently!
Threads in Java Monitors Remote Method Invocation
An airport display
Example
Example A client process running on the
A display showing the arrival or computer of the airline operator
departure times of aircraft at an can
airport. It can be used by all the
add an info line by calling
airlines to publish info about their
addRow
flights.
remove an info line by calling
deleteRow
Multiple clients might call
addRow and deleteRow at
the same time
Several processes will serve
these calls concurrently!
Threads in Java Monitors Remote Method Invocation
An airport display
Example
Example A client process running on the
A display showing the arrival or computer of the airline operator
departure times of aircraft at an can
airport. It can be used by all the
add an info line by calling
airlines to publish info about their
addRow
flights.
remove an info line by calling
deleteRow
Multiple clients might call
addRow and deleteRow at
the same time
Several processes will serve
these calls concurrently!
Threads in Java Monitors Remote Method Invocation
An airport display
Example
Example A client process running on the
A display showing the arrival or computer of the airline operator
departure times of aircraft at an can
airport. It can be used by all the
add an info line by calling
airlines to publish info about their
addRow
flights.
remove an info line by calling
deleteRow
Multiple clients might call
addRow and deleteRow at
the same time
Several processes will serve
these calls concurrently!
Threads in Java Monitors Remote Method Invocation
Example
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface HighLevelDisplay extends Remote{
public void clear() throws RemoteException;
public void addRow(String str)
throws RemoteException;
public void deleteRow(int row)
throws RemoteException;
}
Example
public class JDisplay2 implements HighLevelDisplay {
...
public void addRow(String str) {
updateRow(usedRows,str);
flashRow(usedRows,1000);
usedRows++;
}
...
Example
public class JDisplay2 implements HighLevelDisplay {
...
public void addRow(String str) {
updateRow(usedRows,str);
flashRow(usedRows,1000);
usedRows++;
}
...
Example
public class JDisplay2 implements HighLevelDisplay {
...
public void addRow(String str) {
updateRow(usedRows,str);
flashRow(usedRows,1000);
usedRows++;
}
...
Example
public static void main(String args[]){
try{
HighLevelDisplay hld = new JDisplay2();
HighLevelDisplay stub =
(HighLevelDisplay)UnicastRemoteObject.exportObject(hld,0);
java.rmi.registry.Registry reg =
LocateRegistry.getRegistry();
reg.bind("AirportDisplay",stub);
System.err.println("Display ready");
}catch(Exception e){...}
}
Threads in Java Monitors Remote Method Invocation
Example
public static void main(String args[]){
try{
HighLevelDisplay hld = new JDisplay2();
HighLevelDisplay stub =
(HighLevelDisplay)UnicastRemoteObject.exportObject(hld,0);
java.rmi.registry.Registry reg =
LocateRegistry.getRegistry();
reg.bind("AirportDisplay",stub);
System.err.println("Display ready");
}catch(Exception e){...}
}
Threads in Java Monitors Remote Method Invocation
The java RMI runtime (doing a lot behind the scenes!) has to be
able to receive remote invocations. We say that the object is
exported:
HighLevelDisplay stub =
(HighLevelDisplay)UnicastRemoteObject.exportObject(hld,0);
The runtime may begin to listen on a new server socket or may use
a shared server socket to accept incoming remote calls for the
remote object.
Threads in Java Monitors Remote Method Invocation
The java RMI runtime (doing a lot behind the scenes!) has to be
able to receive remote invocations. We say that the object is
exported:
HighLevelDisplay stub =
(HighLevelDisplay)UnicastRemoteObject.exportObject(hld,0);
The runtime may begin to listen on a new server socket or may use
a shared server socket to accept incoming remote calls for the
remote object.
Threads in Java Monitors Remote Method Invocation
Example
Registry reg = LocateRegistry.getRegistry();
reg.bind("AirportDisplay",stub);
Example
Registry reg = LocateRegistry.getRegistry();
reg.bind("AirportDisplay",stub);
Example
Registry reg = LocateRegistry.getRegistry();
reg.bind("AirportDisplay",stub);
Example
Registry reg = LocateRegistry.getRegistry();
reg.bind("AirportDisplay",stub);
A client
Example
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public static void main(String [] args) {
try{
Registry reg =
LocateRegistry.getRegistry(args[0]);
HighLevelDisplay d =
(HighLevelDisplay)reg.lookup("AirportDisplay");
...
d.addRow("hej");
Threads in Java Monitors Remote Method Invocation
You need the interface for the remote object to compile both the
server class and the client class:
You need the interface for the remote object to compile both the
server class and the client class:
You need the interface for the remote object to compile both the
server class and the client class:
In one machine:
In the other machine:
> rmiregistry &
> java Client
> java JDisplay2
Threads in Java Monitors Remote Method Invocation
In one machine:
In the other machine:
> rmiregistry &
> java Client
> java JDisplay2
Threads in Java Monitors Remote Method Invocation
In one machine:
In the other machine:
> rmiregistry &
> java Client
> java JDisplay2
Threads in Java Monitors Remote Method Invocation