Exemple - String print


class TwoStrings {
     void print(String str1,String str2) {
        System.out.print(str1);
        try {
            Thread.sleep(50);
        } catch (InterruptedException ie) {        }
        System.out.println(str2);
    }
}

class PrintStringsThread extends Thread {
     String str1, str2;
     TwoStrings ts;
     PrintStringsThread(String str1, String str2,     TwoStrings ts) {
        this.str1 = str1;
        this.str2 = str2;
        this.ts = ts;
        this.start(); 
    }
    public void run() {
            ts.print(str1, str2);
    }
}

public class App {
    public static void main(String args[]) {
         TwoStrings ts = new TwoStrings();
        new PrintStringsThread("Hello ", "there.",ts);
        new PrintStringsThread("How are ", "you?",ts);
        new PrintStringsThread("Thank you ", "very much!",ts);
    }
}
How are Hello Thank you there.
you?
very much!

 

Méthode synchronisée

class TwoStrings {
synchronized void print(String str1,String str2) {
System.out.print(str1);
try {
Thread.sleep(50);
} catch (InterruptedException ie) { }
System.out.println(str2);
}
}

class PrintStringsThread extends Thread {
String str1, str2;
TwoStrings ts;
PrintStringsThread(String str1, String str2, TwoStrings ts) {
this.str1 = str1;
this.str2 = str2;
this.ts = ts;
this.start();
}
public void run() {
ts.print(str1, str2);
}
}

public class App {
public static void main(String args[]) {
TwoStrings ts = new TwoStrings();
new PrintStringsThread("Hello ", "there.",ts);
new PrintStringsThread("How are ", "you?",ts);
new PrintStringsThread("Thank you ", "very much!",ts);
}
}
Hello there.
Thank you very much!
How are you?

 

 
Objet synchronisé

class TwoStrings {
void print(String str1,String str2) {
System.out.print(str1);
try {
Thread.sleep(50);
} catch (InterruptedException ie) { }
System.out.println(str2);
}
}

class PrintStringsThread extends Thread {
String str1, str2;
TwoStrings ts;
PrintStringsThread(String str1, String str2, TwoStrings ts) {
this.str1 = str1;
this.str2 = str2;
this.ts = ts;
this.start();
}
public void run() {
synchronized (ts) {
ts.print(str1, str2);
}
}
}

public class App {
public static void main(String args[]) {
TwoStrings ts = new TwoStrings();
new PrintStringsThread("Hello ", "there.",ts);
new PrintStringsThread("How are ", "you?",ts);
new PrintStringsThread("Thank you ", "very much!",ts);
}
}
Hello there.
How are you?
Thank you very much!

 


Exemple - pont à sens unique

pont - resource

public class Bridge {
    private int nVh;
    Bridge(){
        nVh = 0;
    }
    synchronized public int brN(){
        return nVh;
    }
    synchronized public void takeB(boolean lr ){
        while((nVh>0)&& (lr==true)||
              (nVh<0) && (lr==false)){
            System.out.println("\t"+Thread.currentThread().getName()+" waiting");
            try{     wait();   }
            catch(InterruptedException e){
                System.err.println(e);
            }
        }
        if (lr) nVh--;
        else nVh++;
        System.out.println(Thread.currentThread().getName()+" on the bridge");
    }
    synchronized public void leaveB(boolean lr ){
        if (nVh>0) nVh--;
        else nVh++;
        System.out.println("\t\t"+Thread.currentThread().getName()+" leave the bridge");
        notifyAll();
    }
}

voitures - threads

public class Vehicle extends Thread{
    boolean lr;
    Bridge b;
    String name;
    static int num;
    Vehicle(boolean lr, Bridge b){
        this.lr=lr;
        this.b = b;
        name = "V "+ ++num + (lr?" left->":" <-right");
        super.setName(name);
    }
    public void run(){
        b.takeB(lr);
        try {
            sleep((int)(Math.random()*200));
        } catch (InterruptedException e){} 
        b.leaveB(lr);
    }
}

Le modèle:

public class Circ {
    public static void main(String arg[]){
        Bridge b = new Bridge();
        for(int i = 0; i < 6; i++){
            (new Vehicle(true, b)).start();
            (new Vehicle(false,b)).start();
        }
    }
}

 

Exemple

Deux ponts à  sens unique. Une place sans restriction des places entre les deux. Gênerez  un  nombre par hasard des voitures qui doivent passer. Chaque voiture attend un temps par hasard avant de passez et perd un certain temps en passant. Pour entrer sur le pont il ne faut pas avoir des voitures qui roulent en sens inverse.  Faire un programme en Java pour modeler la situation