Figure 1 : Technique de verrouillage synchronisée
class testClass2 {
private double i, j, sum;
.
.
.
public synchronized double putEye (double i) {
this.i=i;
}
public synchronized void putJay (double j) {
this.j=j;
}
public synchronized void sumEyeJay (double i, double j) {
this.sum=i+j;
}
public synchronized void
printEyeJay (double i, double j) {
System.out.println(« i= « + i + » and » + « j= » +j);
}
.
.
.
}
Figure 2 : Technique de verrouillage par bloc synchronisée en utilisant « this »
class testClass2 {
private double i, j, sum;
private Object eyeLock = new Object();
private Object jayLock = new Object();
.
.
.
public void putEye (double i) {
synchronized (eyeLock) {
this.i=i;
}
}
public void putJay (double j) {
synchronized (jayLock) {
this.j=j;
}
}
public synchronized void sumEyeJay (double i, double j) {
synchronized (eyeLock) {
synchronized (jayLock) {
This.sum=i+j;
}
}
}
public synchronized void printEyeJay (double i, double j) {
synchronized (eyeLock) {
synchronized (jayLock) {
System.out.println("i= "+ i + " and " + "j= " +j);
}
}
}
.
.
.
}
Figure 3 : Technique de verrouillage séparant les objets verrou
package poolPkg;
.
.
.
public class myConnect implements Connection {
private myConnectPool pool;
private Connection conn;
private boolean busy;
private long timestamp;
public myConnect(Connection conn, myConnectPool pool) {
this.conn=conn;
this.pool=pool;
this.busy=false;
this.timestamp=à˜;
}
public synchronized boolean assignConn() {
if(busy) {
return false;
} else {
busy=true;
timestamp=System.currentTimeMillis();
return true;
}
}
public boolean validate() {
try {
return(conn.isClosed());
}catch (Exception e) {
return false;
}
}
public boolean inUse() {return busy;}
public long getLastUse() {return timestamp;}
public void close() throws SQLException {pool.returnConnection(this);}
protected void revokeConn() {busy=false;}
protected Connection getConnection() {return conn;}
.
.
.
Figure 4 : Code lourdement synchronisé
package poolPkg;
.
.
.
public class myConnectPool {
private Vector myConnectPool;
private String url, user, password;
final private long timeout=6à˜à˜à˜à˜;
private ConnectionDestroyer destroyer;
final private int poolsize=25;
public myConnectPool(String url, String user, String password) {
this.url = url;
this.user = user;
this.password = password;
myConnectPool = new Vector(poolsize);
destroyer = new ConnectionDestroyer(this);
destroyer.start();
}
public synchronized void destroyConnections() {
long stale = System.currentTimeMillis() - timeout;
Enumeration connlist = myConnectPool.elements();
while((connlist != null) && (connlist.hasMoreElements())) {
myConnect poolConn = (myConnect)connlist.nextElement();
if((poolConn.inUse()) && (stale >poolConn.getLastUse()) &&
(!poolConn.validate())) {
removeConnection(poolConn);
}
}
}
public synchronized void closeConnections() {
Enumeration connlist = myConnectPool.elements();
while((connlist != null) && (connlist.hasMoreElements())) {
myConnect poolConn = (myConnect)connlist.nextElement();
removeConnection(conn);
}
}
private synchronized void removeConnection(myConnect conn) {
myConnectPool.removeElement(conn);
}
public synchronized Connection getConnection() throws SQLException {
myConnect c;
for(int i = à˜; i < myConnectPool.size(); i++) {
c = (myConnect)myConnectPool.elementAt(i);
if (c.assignConn()) { return c;}
}
Connection conn = DriverManager.getConnection(url,user,password);
c = new myConnect(conn, this);
c.assignConn();
myConnectPool.addElement(c);
return c;
}
public synchronized void returnConnection(myConnect conn) {
conn.revokeConn();
}
}
.
.
.
Figure 5 : Code lourdement synchronisé scindé en verrouillages multiples
package poolPkg;
.
.
.
class ConnectionDestroyer extends Thread {
private myConnectPool connPool;
private final long destroyDelay=6à˜à˜à˜à˜à˜;
ConnectionDestroyer(myConnectPool connPool) {
this.connPool=connPool;
}
public void run() {
while(true) {
try {
sleep(destroyDelay);
} catch( InterruptedException e) { }
connPool.destroyConnections();
}
}
}
.
.
.
Téléchargez gratuitement cette ressource
Comment cerner la maturité digitale de votre entreprise ?
Conçu pour les directions IT et Métiers, ce guide vous permettra d'évaluer précisément vos
processus de communication client, d'identifier vos lacunes et points d'inflexion pour établir
un plan d’actions capable de soutenir durablement votre évolution. Bénéficiez maintenant
d'une feuille de route complète.