Download Synchronization

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
2.5. Synchronization
Threads are user created and user controlled, and user must provide a control
mechanism, referred to as synchronization, that prevents resolves conflicts
between the threads while they are executing
Example: perfectly fine code if sequential
int balance=INITIAL;
boolean transfer(int deposit, int withdrawal){
balance += deposit;
if(balance - withdrawal >= 0){
balance -= withdrawal;
return true;
}
return false;
}
When executed in more than one thread may lead to problems, e.g. two threads:
thread 1
thread 2
balance
transfer(0,130)
transfer(0,90)
200
200-130>=0
200
200-90>=0
200
200 -= 90
110
2003
CS 667: Synchronization
1
200Spring
-= 130
-20
Zlateva
Critical Section, Mutual Exclusion, and synchronized
Obviously, in order to prevent this confusion, the segment of code including the
test and the withdrawal, referred to as critical segment/section, must be
executed in its entirety while all other threads are barred from executing the
method. Java provides the synchronized tag that can be applied to
1. a code segment/block of an
synchronized object
synchronized (anObject){
//code for critical section
}
2. a method in a class
synchronized aMethod {
//code for aMethod
}
2-Special Case: a static method in a class
static synchronized aMethod {
//code for aMethod
}
Spring 2003
CS 667: Synchronization
Zlateva
2
Semaphores or Monitors
Every Java object has an associated semaphore.
Semaphores are special system objects (no Java objects!) with two operations:
• get
• release
Only one thread can get the object semaphore. All threats that attempt to get the
semaphore while it is owned by another thread will be put to sleep. When
thread releases object semaphore, one of the waiting threads will succeed and
get it
Semaphore
Thread 1
Thread 2
Spring 2003
get()
release()
get()
release()
CS 667: Synchronization
Zlateva
3
Synchronization Mechanism
The Java run time environment makes sure only one thread executes the
critical statements of anObject.
Once a thread is started in anObject, the object becomes locked. When
another thread tries to get started in the same object it cannot as the object is
locked. Note that not only must the synchronized section be executed in its
entirety, but no other thread at this time can execute these statements. This is
accomplished with semaphores, provided by the OS.
In all cases it is the object that is locked, not the segment or the method!!
Note: This is a version of the problem of mutual exclusion or critical section
or how to provide a user with access to a single resource or how to allow
only one segment of code to be executed at a time without yielding to other
program segments
Spring 2003
CS 667: Synchronization
Zlateva
4
synchronized (object)
1. Critical section is placed within a
synchronized(object){ }
statement.
Example:
int balance = INITIAL;
boolean transfer(int deposit, int withdrawal){
refers to object on which
method is called
synchronized(this) {
balance += deposit
if(balance - withdrawal >= 0){
balance -= withdrawal;
return true;
}
return false;
}
}
Spring 2003
CS 667: Synchronization
Zlateva
5
synchronized aMethod
2. Method is declared synchronized. Here, the thread must obtain semaphore
on the object (or class in case of static methods) on which method is called.
Example:
int balance=INITIAL;
synchronized boolean transfer(int deposit, int withdrawal){
balance += deposit
if(balance - withdrawal >= 0){
balance -= withdrawal;
return true;
}
return false;
}
Note: 1 and 2 are equivalent.
Spring 2003
CS 667: Synchronization
Zlateva
6
static synchronized aMethod
2-Special Case: a static method in a class
static synchronized aMethod {
//code for aMethod
}
Question: Static methods are for the entire class, not for a particular instance.
What object does the static method lock?
Answer: the class object Singleton.class. In Java there is a unique object of
type Class that describes each class the virtual machine has loaded. If a thread
calls a synchronized method of a class all static synchronized methods of the
class are blocked until the first call returns.
Application: Using static synchronized is appropriate for getInstance() methods
of singleton classes. A singleton is a class with just one object. It is useful for
objects that must be globally unique. Typical examples are management
objects such as spoolers, DB connection managers, etc. Synchronizing the
getInstance() method makes sure there is only one instance created.
Spring 2003
CS 667: Synchronization
Zlateva
7
wait, notify, notifyAll methods
Problem: thread has gained ownership of an object and runs the synchronized
transfer method. There is not enough money in the account, but it may be
replenished if deposit made.
Goal: Provide ability to release object lock and wait for better time
Solution:
The java.lang package provides the methods wait and notify/notifyAll for
relinquishing object locks and attempt to get them back later (note these are
java.lang methods, not Thread instance methods.)
the wait method is called from within the synchronized segment and causes the
thread to go into blocked state and release the object lock.
after wait the thread is put on a wait list, where it stays and can be removed
only when another object calls notify or notifyAll
notify removes one arbitrarily chosen object from the wait list
notifyAll removes all objects from the wait list
It is crucial that notify or notifyAll is called, as a thread cannot unblock itself.
If the waiting thread is not unblocked by another thread the computation is in
Spring 2003
CS 667: Synchronization
8
deadlock
Zlateva
wait, notify, notifyAll methods (continued)
When threads are removed from the wait list the scheduler will activate them
according to OS policies: therefore it may be dangerous to use notify as there is
no control what object is removed from the wait list, e.g. if the wrong thread is
unblocked it may not be able to proceed and cause deadlock. It is therefore
safer to always use notifyAll.
Recommendation: call notifyAll always when the state of objects changes in a
way that may be advantageous to waiting calls, e.g. whenever balance changes
int balance=INITIAL;
synchronized boolean transfer ( int deposit, int withdrawal){
balance += deposit
while(balance - withdrawal < 0)
wait();
balance -= withdrawal;
notifyAll();
return true;
}
return false;
}
Spring 2003
CS 667: Synchronization
Zlateva
9
Beware of Selfish Threads
A thread that does not want/need to put itself to sleep and does not call yield
thus threatening to monopolize the system is called a selfish thread.
A thread should always yield or sleep when executing a long computation
Spring 2003
CS 667: Synchronization
Zlateva
10
Examples for In-Class Discussion
PiggyBankWithoutSync.java
PiggyBankWithSync.java
UnsynchBankTest.java
SynchBankTest.java
Spring 2003
CS 667: Synchronization
Zlateva
11
Deadlock
A situation in a multithreaded application when no thread can proceed and the
application hangs.
Example:
all threads are trying to transfer a larger amount than they have in their
accounts.
Is this likely?
In the SynchBankTest.java change the maximal amount allowed for transfer
from 10,000 e.g. to 15,000 (more than the initial balance) and see if deadlock
occurs.
Spring 2003
CS 667: Synchronization
Zlateva
12
Daemons
What is it? A daemon is a thread whose only purpose in life (run time) is to
serve other threads (like the daemon/jini in the bottle, and, no, they were not
invented with Java).
Thus it makes sense for a daemon to terminate when it has nobody to serve
anymore.
How is a daemon created? By invoking the following method before thread is
started:
setDaemon ( true);
How is daemon different from other threads? When only daemon threads
remain the program terminates automatically.
Why is it useful? Provides general service in the background while the program
is running, but does not do application specific work. When all worker threads
have terminated one needs not to worry about closing the application - the JVM
exits once only daemons remain alive.
Spring 2003
CS 667: Synchronization
Zlateva
13
Daemons (continued)
General Syntax:
public final void setDaemon (boolean on)
Marks this thread as
• daemon thread for on = true
• user thread for on = false
The Java Virtual Machine exits when the only threads running are all daemon
threads.
This method must be called before the thread is started.
Throws: IllegalThreadStateException
if the thread is active.
For examples see (Eck 2000, Deamon Threads in Ch.14, p.840ff).
Spring 2003
CS 667: Synchronization
Zlateva
14
Thread Safe Methods and Swing
What is a thread safe method? A method that will not corrupt the application
when used with threads. In other words a method that has been synchronized.
Surprise: Most Swing methods are not synchronized, thus not thread safe!
This seems a gross oversight, but there are two good reasons:
- synchronization = overhead -> slows down application, and Swing needs no
additional slow down;
- prior experience with thread safe methods indicates that synchronization
demands are confusing and when programmers extended/customized the thread
safe methods the result was often components that were prone to deadlock.
Some Swing methods are safe: Thread safe methods are marked in the
documentation with the sentence: "This method is thread safe, although most
Swing methods are not." , e.g.
JTextComponent.setText also the methods of the JComponent class
JTextArea.insert
repaint
JTextArea.append
revalidate
JTextArea.replaceRange
Spring 2003
CS 667: Synchronization
Zlateva
15
2. 6. Accessing Web Services
Example 1: Sending e-mail (HoCo, Ch.3, Figure 3-7)
1. Open socket to SMTP port (25) of your host
Socket s = new Socket("mail.yourserver.com", 25);
PrintWriter out = new PrintWriter(s.getOutputStream());
2. Send following commands and information
HELO sending host name
MAIL FROM sending address
RCPT TO: recipient address
DATA
message
.
\n . marks end of message
Spring 2003
send("HELO " + hostName);
send("MAIL FROM: <" + from.getText() + ">");
send("RCPT TO: <" + to.getText() + ">");
send("DATA");
StringTokenizer tokenizer =
new StringTokenizer( message.getText(), "\n");
while (tokenizer.hasMoreTokens())
send(tokenizer.nextToken());
send(".");
s.close;
CS 667: Synchronization
Zlateva
16
Example 2: HTTP Server
HTTP server:
Code for GET
Classes:
Web browser
Request for Service: GET,
POST, HEAD
http extends Thread: has
main
run method loops forever listening to
client requests
Connect extends Thread : handles all
communication and has
run gets client requests and provides
service, e.g. locating and shipping file
shipDocument
The HTTP server normally runs on port 80 and examples using GET are
GET http://metcs.bu.edu/~zlateva/
HTTP/1.0
GET http://host.domain/doc.html HTTP/1.0
In the first example a directory rather than specific document is given and the browser
will try to load index.html, that is typically in the root directory. Whether it really will
find it depends on server configuration.
Spring 2003
CS 667: Synchronization
Zlateva
17
Security HTTP Server
Problem: How can we put restrictions on what files the HTTP server can release to
the client?
Java provides the SecurityManager class that can be customized for the
application as follows:
(i) Create customized subclass ourHttpSecurityManager of SecurityManager:
class HttpSecurityManager extends SecurityManager {
public void checkAccess ( Thread t ){ };
… //methods overriding base class methods Returns the index within string
filename of the first occurrence of
public void checkRead ( String filename) { the specified substring ".." and
throws exception if it is not the end
of string, i.e. will deny all requests
with ".." in path.
if (filename.indexOf("..") != -1) {
throw new SecurityException
("Not enough privileges to read:" + filename)
}
}
}
Spring 2003
CS 667: Synchronization
Zlateva
18
SecurityManager class - API specification
SecurityManager
abstract class that allows applications to implement a security policy. It allows an
application to determine, before performing a possibly unsafe or sensitive
operation, what the operation is and whether the operation is being performed by a
class created via a class loader rather than installed locally. Classes loaded via a
class loader (especially if they have been downloaded over a network) may be less
trustworthy than classes from files installed locally. The application can allow or
disallow the operation.
Has many methods that start with check; the ones used in our example are:
public void checkAccess ( Thread t )
Throws a SecurityException if the calling thread is not allowed to modify the
thread argument.
public void checkRead ( String filename)
Throws a SecurityException if the calling thread is not allowed to read the file
specified by the string argument.
Spring 2003
CS 667: Synchronization
Zlateva
19
Security HTTP Server (continued)
(ii) Install security manager in main of server code with
System.setSecurityManager(new OurHttpSecurityManager())
And make sure to catch SecurityException in method that is sending the requested
file, e.g.
public static void shipDocument (….){
try{
….
} catch (SecurityException e){
out.writeBytes("403 Forbidden");
Spring 2003
CS 667: Synchronization
Zlateva
20
3. Firewalls and Proxy Servers
Goal: Protect/Control access to site
Approach: Interpose system (software or hardware), called firewall,
between network traffic and site
Packet filters
at IP level
Corporate
Network
Proxy Server
Application level
Gateway
Spring 2003
F
i
r
Internet Traffic
e
w
a
l
l
CS 667: Synchronization
Zlateva
21
Firewalls and Proxy Servers (continued)
Firewalls are typically implemented in two flavors:
(i) Packet filters:
• done at IP level, typically at routers with external interfaces;
• deny/allow access of packet, according to access control list based on
source& destination address of packet;
• attempts to reach hosts behind firewall are blocked or logged.
(ii) Application level Gateway or Proxies
• server interposed between client and server
• handles connection between client and server disallowing direct
communication
Characteristics:
• only proxy IP known to outside world  protects internal network from
attacks on internal machine
• restrict access thus increasing safety; however, this is done by
eliminating direct streams between client and server, thus restricting
communication only to services supported by the proxy: this may in effect
prevent a number of applications to work through firewall (e.g. some
streaming video applications).
For a simple proxy server see Example 3-9, HoCo 2002, p.255,ff)
Spring 2003
CS 667: Synchronization
Zlateva
22
4. Serialization
Goal: Make objects live beyond life time of program, i.e. instead of being
transient the object becomes persistent
Approach:
• Serialization transforms objects into byte sequences, that are stored in
files, and from which one can fully reconstruct the original object. This
can be done across network and operating systems, i.e. and an object
created on a Unix machine can be serialized and then reconstructed on a
remote Windows machine;
• Object serialization implements a lightweight persistence:
persistence because the stored byte sequence of the object exists beyond
program lifetime;
lightweight because the programmer has to take care of all the details of
serialization; as opposed to defining an object as persistent through some
"persistent" keyword and letting the system handle all the details.
Spring 2003
CS 667: Synchronization
Zlateva
23
Serialization (continued)
Serialization was introduced to support
• Remote Method Invocation (RMI) that allows to perform operations on
remote objects. Object serialization is needed to transport the arguments
to the remote machined and then to return the value.
• JavaBeans (component programming model for easy creation of visual
programming environments) When a Bean is created state info is
generally configured at design time; this state info must be stored and
later recovered when the program is started - a task performed by
serialization.
Spring 2003
CS 667: Synchronization
Zlateva
24
4.1. Serializing Object - Serializable Interface
(i) Mark object to be serialized: it must implement the java.io.Serializable
interface
Example:
public class OurSerializableClass implements Serializable
Note: For a class to implement the Serializable interface, when its superclass
is not serializable, the superclass must provide a no argument constructor.
The Serializable interface
• is a marker interface: it indicates that the class that implements it may be
serialized by the default serialization methods; (customized serialization
can be provided in addition to the default serialization
• contains no methods or data
• simply a flag that lets object streams know serialization instances of the
class will not reveal sensitive information or open security holes.
Example:
OurSerializableClass o1 = new OurSerializableClasss();
Spring 2003
CS 667: Synchronization
Zlateva
25
Example:
SaveStu
+ main()
ReadStu
+ main()
1
1
*
*
StudentPersist implements Serializable
-String name;
-int id;
+ StudentPersist( String name, int id)
+ void print()
Spring 2003
CS 667: Synchronization
Zlateva
26
Example: Serializable
import java.io.*;
/**
* @(#)StudentPersist.java
* Serialized class for student records
*/
public class StudentPersist implements Serializable {
String name;
int id;
public StudentPersist( String name, int id){
this.name = name;
this.id = id;
}
public void print(){
System.out.println("Record for: " + name);
System.out.println("ID: " + id);
}
} Spring 2003
CS 667: Synchronization
Zlateva
27
Serializing Object - Save Objects
(ii) Save Object: Create instance of ObjectOutputStream which is a
subclass of OutputStream and has a writeObject method:
Example:
FileOutputStream (String name)
Creates an input file stream to write to a file with the
specified name.
FileOutputStream fos = new FileOutputStream("outputFileName");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(o1);
writeObject(Object obj)
Write the specified object to
the ObjectOutputStream..
Spring 2003
ObjectOutputStream(OutputStream out)
Creates an ObjectOutputStream that writes to the
specified OutputStream.
CS 667: Synchronization
Zlateva
28
Save Objects - API specifications
public class ObjectOutputStream extends OutputStream
implements ObjectOutput, ObjectStreamConstants
• An ObjectOutputStream writes primitive data types and graphs of
Java objects to an OutputStream.
• The method writeObject is used to write an object to the stream.
• Any object, including Strings and arrays, is written with writeObject.
Multiple objects or primitives can be written to the stream.
• The objects must be read back from the corresponding
ObjectInputStream with the same types and in the same order as
they were written.
• Primitive data types can also be written to the stream using the
appropriate methods from DataOutput.
Spring 2003
CS 667: Synchronization
Zlateva
29
Example: Save Objects
import java.io.*;
/**
* @(#) SaveStu.java
* Serializes student records
*/
public class SaveStu {
public static void main(String argv[]) throws Exception {
//create two records
StudentPersist james = new StudentPersist("James Norton", 111111);
StudentPersist chris = new StudentPersist("Christine Price", 222222);
//serialize
FileOutputStream fos = new FileOutputStream("list");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(james);
oos.writeObject(chris);
oos.flush();
}
}
Spring 2003
CS 667: Synchronization
Zlateva
30
Serializing Object - Read Objects
(iii) Read Object: Create instance of ObjectInputStream which is a subclass of
InputStream and has a readObject method:
Example:
FileInputStream (String name)
Creates an input file stream to read from a file with
the specified name.
FileInputStream fis = new FileInputStream("inputFileName");
ObjectInputStream ois = new ObjectInputStream ( fis );
OurSerializableObject o2 = (OurSerializableObject) ios.readObject( );
readObject( )
Read an object from the
ObjectInputStream.
ObjectInputStream(InputStream in)
Creates an ObjectInputStream that
reads from the specified InputStream.
Spring 2003
CS 667: Synchronization
Zlateva
31
Read Objects API specifications
public class ObjectInputStream extends InputStream
implements ObjectInput, ObjectStreamConstants
• An ObjectInputStream deserializes primitive data and objects
previously written using an ObjectOutputStream.
• ObjectOutputStream and ObjectInputStream can provide an application
with persistent storage for graphs of objects when used with a
FileOutputStream and FileInputStream respectively.
• ObjectInputStream is used to recover those objects previously serialized.
Other uses include passing objects between hosts using a socket stream
or for marshaling and unmarshaling arguments and parameters in a
remote communication system.
• ObjectInputStream ensures that the types of all objects in the graph
created from the stream match the classes present in the Java Virtual
Machine. Classes are loaded as required using the standard mechanisms.
Spring 2003
CS 667: Synchronization
Zlateva
32
Read Objects API specifications (continued)
• Only objects that support the java.io.Serializable or
java.io.Externalizable interface can be read from streams.
• The method readObject is used to read an object from the stream. Java's
safe casting should be used to get the desired type. In Java, strings and
arrays are objects and are treated as objects during serialization. When
read they need to be cast to the expected type.
• Primitive data types can be read from the stream using the appropriate
method on DataInput.
• The default deserialization mechanism for objects restores the contents of
each field to the value and type it had when it was written. Fields
declared as transient or static are ignored by the deserialization process.
References to other objects cause those objects to be read from the
stream as necessary. Graphs of objects are restored correctly using a
reference sharing mechanism. New objects are always allocated when
deserializing, which prevents existing objects from being overwritten.
Spring 2003
CS 667: Synchronization
Zlateva
33
Copyright (C) 1996-99 Symantec Corporation
Example: Read Objects
import java.io.*;
Record for: James Norton
/**
ID: 111111
* @(#) ReadStu.java
Record for: Christine Price
* Deserializes student records
ID: 222222
*/
press any key to exit...
public class ReadStu {
public static void main(String argv[]) throws Exception {
//deserialize
FileInputStream fis = new FileInputStream("list");
ObjectInputStream ois = new ObjectInputStream(fis);
StudentPersist jn = (StudentPersist) ois.readObject();
StudentPersist ep = (StudentPersist) ois.readObject();
jn.print();
ep.print();
}
}
Spring 2003
CS 667: Synchronization
Zlateva
34
Security in Object Serialization
Problem: Marking and object as serializable = no sensitive information,
object can be viewed by anybody. Obviously a problem for some objects,
e.g. for passwords
Solutions:
1. Mark sensitive fields as transient:
private transient String passwd;
As pointed out in previous slide the serialization process will ignore all
transient and static objects
2. Implement Externalizable interface: object streams write only the class
name to the stream – this is useful when one wants to provide entirely
manual serialization facilities.
Externalizable extends the Serializable interface and has two methods:
writeExternal and readExternal that must be implemented.
Note: writeExternal and readExternal are public methods and thus are not
entirely secure.
3. Encrypt the bytestream produced by the serialization process
Spring 2003
CS 667: Synchronization
Zlateva
35
Controlling the Serialization - Externalizable Interface
Goal: Control/Determine what should be serialized and what should not,
instead of the default serialization
Externalizable extends Serializable
• Externalization allows a class to specify the methods to be used to write
the object's contents to a stream and to read them back. The
Externalizable interface's writeExternal and readExternal methods are
implemented by a class to give the class complete control over the format
and contents of the stream for an object and its supertypes. These
methods must explicitly coordinate with the supertype to save its state.
• When an Externalizable object is to be reconstructed, an instance is
created using the public no-arg constructor and the readExternal method
called.
Spring 2003
CS 667: Synchronization
Zlateva
36
Externalizable Interface
An Externalizable object must implement the following methods:
public void writeExternal(ObjectOutput) throws IOException
The object implements the writeExternal method to save its contents by calling
the methods of DataOutput for its primitive values or calling the writeObject
method of ObjectOutput for objects, strings and arrays.
public void readExternal(ObjectInput) throws IOException
The object implements the readExternal method to restore its contents by
calling the methods of DataInput for primitive types and readObject for objects,
strings and arrays.
Examples:
public void writeExternal(ObjectOutput objout) throws IOException {
….
obbjout.writeObject(password);
….
}
public void readExternal(ObjectInput objin) throws IOException,
ClassNotFoundException {
….
this.passwd = (String) objin.readObject(passwd);
….
} Spring 2003
CS 667: Synchronization
37
Zlateva