> Tech > Figures

Figures

Tech - Par Renaud ROSSET - Publié le 24 juin 2010
email

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 cette ressource

Guide inmac wstore pour l’équipement IT de l’entreprise

Guide inmac wstore pour l’équipement IT de l’entreprise

Découvrez toutes nos actualités à travers des interviews, avis, conseils d'experts, témoignages clients, ainsi que les dernières tendances et solutions IT autour de nos 4 univers produits : Poste de travail, Affichage et Collaboration, Impression et Infrastructure.

Tech - Par Renaud ROSSET - Publié le 24 juin 2010