Exemples: threads

Vacation.java
public class Vacation{
    public static void main (String[] args) {
	new SimpleThread("Jamaica").start();
	new SimpleThread("Fiji").start();
	new SimpleThread("Longueuil").start();
    }
}


class SimpleThread extends Thread {
    public SimpleThread(String str) {
	super(str);
    }
    public void run() {
	for (int i = 0; i < 10; i++) {
	    System.out.println(i + " " + getName());
	    try {
		sleep((long)(Math.random() * 1000));
	    } catch (InterruptedException e) {}
	}
    }
}



AnnoyingBeep.java
import java.util.Timer;
import java.util.TimerTask;
import java.awt.Toolkit;

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

public class AnnoyingBeep {
    Toolkit toolkit;
    Timer timer;

    public AnnoyingBeep() {
	toolkit = Toolkit.getDefaultToolkit();
        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) {
	        toolkit.beep();
		System.out.println("Beep!");
		numWarningBeeps--;
	    } else {
	        toolkit.beep(); 
                System.out.println("Time's up!");
	        //timer.cancel(); //Not necessary because we call System.exit
	        System.exit(0);   //Stops the AWT thread (and everything else)
	    }
        }
    }

    public static void main(String args[]) {
	System.out.println("About to schedule task.");
        new AnnoyingBeep();
	System.out.println("Task scheduled.");
    }
}

UnsyncProducerConsumer.java
public class UnsyncProducerConsumer {
    public static void main (String [] args) {
	UnsyncCubbyHole c = new UnsyncCubbyHole();
	
	Consumer consumer = new Consumer(c,1);
	Producer producer = new Producer(c,2);
	
	consumer.start();
	producer.start();
    }
}

class Consumer extends Thread {
    private UnsyncCubbyHole cubbyhole;
    private int number;

    public Consumer(UnsyncCubbyHole c, int number) {
        cubbyhole = c;
        this.number = number;
    }

    public void run() {
        int value = 0;
        for (int i = 0; i < 10; i++) {
            value = cubbyhole.get(number);
            try {
                sleep((int)(Math.random() * 100));
            } catch (InterruptedException e) { }
        }
    }
}

class Producer extends Thread {
    private UnsyncCubbyHole cubbyhole;
    private int number;

    public Producer(UnsyncCubbyHole c, int number) {
        cubbyhole = c;
        this.number = number;
    }

    public void run() {
        for (int i = 0; i < 10; i++) {
            cubbyhole.put(number, i);
            try {
                sleep((int)(Math.random() * 100));
            } catch (InterruptedException e) { }
        }
    }
}

class UnsyncCubbyHole {
    private int contents;
  

    public int get(int who) {
        System.out.println("Consumer " + who + " got: " + contents);
        return contents;
    }

    public void put(int who, int value) {
        contents = value;
        System.out.println("Producer " + who + " put: " + contents);
    }
}

SsyncProducerConsumer.java



public class SyncProducerConsumer {
    public static void main (String [] args) {
	SyncCubbyHole c = new SyncCubbyHole();
	
	Consumer consumer = new Consumer(c,1);
	Producer producer = new Producer(c,2);
	
	consumer.start();
	producer.start();
    }
}

class Consumer extends Thread {
    private SyncCubbyHole cubbyhole;
    private int number;

    public Consumer(SyncCubbyHole c, int number) {
        cubbyhole = c;
        this.number = number;
    }

    public void run() {
        int value = 0;
        for (int i = 0; i < 10; i++) {
            value = cubbyhole.get(number);
            try {
                sleep((int)(Math.random() * 100));
            } catch (InterruptedException e) { }
        }
    }
}

class Producer extends Thread {
    private SyncCubbyHole cubbyhole;
    private int number;

    public Producer(SyncCubbyHole c, int number) {
        cubbyhole = c;
        this.number = number;
    }

    public void run() {
        for (int i = 0; i < 10; i++) {
            cubbyhole.put(number, i);
            try {
                sleep((int)(Math.random() * 100));
            } catch (InterruptedException e) { }
        }
    }
}

class SyncCubbyHole {
    private int contents;
    private boolean available = false;

    public synchronized int get(int who) {
        while (available == false) {
            try {
                wait();
            } catch (InterruptedException e) { }
        }
        available = false;
        System.out.println("Consumer " + who + " got: " + contents);
        notifyAll();
        return contents;
    }

    public synchronized void put(int who, int value) {
        while (available == true) {
            try {
                wait();
            } catch (InterruptedException e) { }
        }
        contents = value;
        available = true;
        System.out.println("Producer " + who + " put: " + contents);
        notifyAll();
    }
}




Last modified: Mon Dec 5 10:51:23 EST 2005 par Douglas Eck [douglas D0T eck AT umontreal D0T ca]