public void run()public void start()run()
void sleep(int milisec)void wait()Thread (et redéfinir la méthode run())Runnable (et définir la méthode run())Dérivation de la classe Thread
class MonThread extends Thread {
public MonThread() {
// initialisation
}
public void run() {
// le code de l'action du thread
}
}
MonThread t = new MonThread();
t.start(); // execute la methode run() de MonThread en
// parallele au thread de ce programme
Implémentation de l'interface Runnable
class MaTache implements Runnable {
public MaTache() {
// initialisation
}
public void run() {
// le code de l'action du thread
}
}
MaTache tache = new MaTache();
Thread t = new Thread(tache) ;
t.start(); // execute la methode run() de MaTache en
// parallele au thread de ce programme
Thread.sleep(long milisec) : endort le thread courantThread.currentThread() : retourne une référence vers le thread
courantThread.yield() : le thread passe son tourThread t = new Thread();
t.start() : démarre le thread
t.join() : bloque le thread appelant tant que t n'est pas terminét.yield() : le thread t passe son tour pour être exécutéObject o
o.wait() : bloque thread qui l'exécute
o.notify() : réveille un des threads qui attendo.notifyAll() : réveille tous les threads qui attendentInterruptedException
Un thread bloqué peut être interrompu via la méthode void interrupt()
class MonThread extends Thread {
public void run() {
try {
Thread.sleep(100);
System.out.println("Je me suis réveillé tout seul");
}
catch (InterruptedException e) {
System.out.println("On m'a réveillé :-(");
}
}
}
MonThread t = new MonThread();
t.start();
Thread.sleep((long) (Math.random()*200));
t.interrupt();
Mot-clé synchronized
public class ExclusionMutuelle {
public static void main(String[] args) throws InterruptedException {
Compte c = new Compte();
Thread[] lesThreads = new Thread[10];
for (int i=0 ; i<lesThreads.length ; i++) {
lesThreads[i] = new Thread() {
public void run() {
for (int i=0 ; i<1000 ;i++) {
c.deposerUnEuros();
}
}
};
lesThreads[i].start();
}
for (Thread x : lesThreads) {
x.join();
}
System.out.println(c.value);
}
}
class Compte {
public volatile int value;
// methode appelée en exclusion mutuelle
// grâce au mot clé synchronized
public synchronized void deposerUnEuros() {
value+=1;
}
}
Devant une méthode (verrouille l'objet sur lequel est appelée la méthode) :
class Compte {
public volatile int value;
public synchronized void deposerUnEuros() { value+=1; }
}
En tant que bloc :
class Compte {
public volatile int value;
public Object lock = new Object();
public void deposerUnEuros() {
synchronized(lock) { value+=1;}
}
}
class MonThread1 extends Thread {
public void run(){
synchronized(a){
. . .
synchonized(b){
. . .
}
}
}
}
class MonThread2 extends Thread {
public void run(){
synchronized(b){
. . .
synchonized(a){
. . .
}
}
}
}
wait() appelée sur un objet o, met le thread appelant en attente
o.wait(), o.wait(long millisec), …synchronized (o) { o..wait() ; } par exempleInterruptedExceptiono.notify() : réveille un des threads en attente (on ne controle pas lequel)o.notifyAll() : réveille tous les threads en attentejava.util.concurrent.SemaphoreSemaphore sem = new Semphore(int nbJetons) ;
sem.acquire() ;
boolean res = sem.tryAcquire() ;
sem.release() ;
class X {
private final ReentrantLock lock = new ReentrantLock();
public void m() {
lock.lock(); // prise de verrou bloquante
try {
// ... code en exclusion mutuelle
} finally {
lock.unlock(); // relâche le verrou
}
}
}
class Y {
private final Object lock = new Object();
// ...
public void m() {
synchronized(lock) { // prise de verrou bloquante
// ... code en exclusion mutuelle
} // relache le verrou
}
}
class Z {
private final ReentrantLock lock = new ReentrantLock();
private final Condition cond = lock.newCondition();
public void arriveeRDV() throws InterruptedException {
lock.lock();
try {
cond.await(); //attendre
} finally {
lock.unlock();
}
}
public void debloquerRDV() throws InterruptedException {
lock.lock();
try {
// reveiller
cond.signal(); //cond.signalAll();
} finally {
lock.unlock();
}
}
}
CyclicBarrier , CountDownLatch