Eviter auto-blockage

Java runtime system permet à la thread d'utiliser un moniteur déjà obtenu (les moniteurs sont  re-entrant). Comme ca on peut éviter autoblocage

 

public class Reentrant {
public static void main(String a[]){
Reentrant r = new Reentrant();
r.a();
}
public synchronized void a() {
b();
System.out.println("here I am, in a()");
}
public synchronized void b() {
System.out.println("here I am, in b()");
}
}
here I am, in b()
here I am, in a()

 

Timers

Classe

java.util.Timer



Constructor Summary
Timer()
          Creates a new timer.
Timer(boolean isDaemon)
          Creates a new timer whose associated thread may be specified to run as a daemon.
Timer(String name)
          Creates a new timer whose associated thread has the specified name.
Timer(String name, boolean isDaemon)
          Creates a new timer whose associated thread has the specified name, and may be specified to run as a daemon.
Method Summary
 void cancel()
          Terminates this timer, discarding any currently scheduled tasks.
 int purge()
          Removes all cancelled tasks from this timer's task queue.
 void schedule(TimerTask task, Date time)
          Schedules the specified task for execution at the specified time.
 void schedule(TimerTask task, Date firstTime, long period)
          Schedules the specified task for repeated fixed-delay execution, beginning at the specified time.
 void schedule(TimerTask task, long delay)
          Schedules the specified task for execution after the specified delay.
 void schedule(TimerTask task, long delay, long period)
          Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay.
 void scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
          Schedules the specified task for repeated fixed-rate execution, beginning at the specified time.
 void scheduleAtFixedRate(TimerTask task, long delay, long period)
          Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay.



java.util.TimerTask



Constructor Summary
protected TimerTask()
          Creates a new timer task.

Method Summary
 boolean cancel()
          Cancels this timer task.
abstract  void run()
          The action to be performed by this timer task.
 long scheduledExecutionTime()
          Returns the scheduled execution time of the most recent actual execution of this task.


Exemple - une seule fois

import java.util.Timer;
import java.util.TimerTask;


public class TimerReminder {
    Timer timer;
    public TimerReminder(int seconds) {
        timer = new Timer();
        timer.schedule(new RemindTask(), seconds*1000);
    }
    class RemindTask extends TimerTask {
        public void run() {
            System.out.println("Time's up!");
            timer.cancel(); //Terminate the timer thread
        }
    }
    public static void main(String args[]) {
        System.out.format("About to schedule task.%n");
        new TimerReminder(4);
        System.out.println("Task scheduled.");
    }
}
About to schedule task.
Task scheduled.
Time's up!


Exemple - boucle 3 fois

import java.util.Timer;
import java.util.TimerTask;

/**
 * Schedule a task that executes once every second.
 */

public class Beep {
    Timer timer;
  
    public Beep() {
        timer = new Timer();
        timer.schedule(new RemindTask(),
                0,        //initial delay
                1*1000);  //subsequent rate
    } 
    class RemindTask extends TimerTask {
        int numWarningBeeps = 3;
      
        public void run() {
            if (numWarningBeeps > 0) {
                System.out.println("Beep!");
                numWarningBeeps--;
            } else {
                System.out.println("Time's up!");
                timer.cancel();
            }
        }
    }
    public static void main(String args[]) {
        System.out.println("About to schedule task.");
        new Beep();
        System.out.println("Task scheduled.");
    }
}
About to schedule task.
Task scheduled.
Beep!
Beep!
Beep!
Time's up!


Exercise 

Un jeu: Si le clique est sur le carré bleu le compteur augmente, si en dehors - le compteur diminue

Ajouter um timer qui deplace le carré a une position aléatoire une fois par seconde




import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Test extends Applet{
    boolean bleu = true;
    int x=30,y=30, rd=30,sz=250;
    Label l = new Label("0");
    int cnt=0;
    public void init(){
        setSize(sz, sz);
        setForeground(Color.blue);
        addMouseListener(new MouseHandler());
        add(l);
    }
    public void paint(Graphics g) {
        g.fillRect(x, y, rd, rd);
    }
    boolean in (int mx,int my){
        if((mx>x) && (mx<x+rd ))
            if((my>y)&&(my<y+rd)) return true;
        return false;
    }
    class MouseHandler extends MouseAdapter {
        public void mousePressed(MouseEvent e){
             if(in(e.getX(),e.getY())) cnt++;
             else cnt--;
             l.setText(cnt+"");
 
        }
    }
}