Lecture 18:

Contents of today's lecture:

An example
Previous Next
A few words about Java applets
  • A Java applet is run by the Java interpreter inside a browser

  • The execution of an applet can be activated from an html document by an APPLET tag like
       <APPLET CODE = "myapplet.class">
           <PARAM NAME = ... VALUE = ...>

  • myapplet should be a subclass of the class Applet (myapplet.class is the result of the compilation of the program declaring such class, myapplet.java)

  • The browser download myapplet.class and creates an object of class myapplet. Then it invokes the method init(), and then start(). While running, the applet may cause other classes to be downloaded.

  • The class Applet has four special method that are "recognized" by the browser and that define the lifecycle of an object: init(), start(), stop() and destroy(). Usually they are overriden in myapplet.

    • init() is called the first time the page is visited
      • initializes the object, typically using the parameters
      • creates the thread(s), if any

    • start() is called each time the page is visited
      • starts the threads (the first time)
      • resumes the threads (the other times)

    • stop() is called each time we leave the page
      • suspends the threads

    • destroy() is called when we leave the page definitively
      • frees up any resource
      • terminates the threads
Previous Next
Using threads in applets
  • There is no multiple inheritance in Java, hence it is not possible to define a class which is at the same time an extension of Thread and of Applet

  • If we need to run a thread in the applet, one way is to define myapplet as an extension of Applet and an implementation of the interface Runnable.
       public class myapplet extends Applet 
                             implements Runnable {...}
    Then we can create a thread with the statement
       new Thread(this)
  • More in general, we can create a thread by using the Thread constructor with a Runnable parameter r, When we start the thread, the method run() executed is the one of r.
     ________          __________          __________           __________
    |        | target |          | implmt |          | extends |          |
    | Thread |------->| Runnable |<.......|  myrunbl |-------->| supercls |      
    |________|        |__________|        |__________|         |__________|
                      |          |        |          |
                      |   run()  |        |   run()  |
                      |__________|        |__________|

    This is an alternative way of creating threads in Java, and it is often preferable to the one we have seen previous time (threads as extensions of Thread), because:

    • more flexible (myrunbl can be an extension of another class)

    • if we need only run(), then inheriting all methods of Thread is an unnnecessary overhead
Previous Next
  • One of the first mechanisms for process synchronization; proposed by Dijkstra in 1968

  • A semaphore s is a non-negative integer variable on with two operations defined on it:

    • P ("passeren", Dutch for "to pass") : if s>0 then s = s-1

    • V ("vrijgeven", Dutch for "to release") : s = s+1

  • Semaphores can be used to ensure that only a certain number of processes can execute a certain critical section at the same time.

  • A process tries to execute a P on s before entering its critical section. If the P cannot be executed because s is 0, then it waits

  • After leaving its critical section a process executes a V on s. At this point one of the processes waiting on s will be resumed. Often (but not necessarily) the mechanism follows a FIFO discipline.
The implementation in Java is the Object of Assignment #6
Previous Next
The example
The program SemaDemo.java
package concurrency.semaphore;

import java.awt.*;
import java.applet.*;
import concurrency.display.*;


class MutexLoop implements Runnable {

    Semaphore mutex;

    MutexLoop (Semaphore sema) {mutex=sema;}

    public void run() {
      try {
        while(true)  {
            // get mutual exclusion
            while(ThreadPanel.rotate()); //critical section
            //release mutual exclusion
      } catch(InterruptedException e){}


public class SemaDemo extends Applet {

    ThreadPanel thread1;
    ThreadPanel thread2;
    ThreadPanel thread3;
    NumberCanvas semaDisplay;

    public void init() {
        setLayout(new BorderLayout());
        semaDisplay = new NumberCanvas("Mutex");
        Panel p = new Panel();
        p.add(thread1=new ThreadPanel("Thread 1",Color.blue,true));
        p.add(thread2=new ThreadPanel("Thread 2",Color.blue,true));
        p.add(thread3=new ThreadPanel("Thread 3",Color.blue,true));

    public void start() {
        Semaphore mutex = new DisplaySemaphore(semaDisplay,1);
        thread1.start(new MutexLoop(mutex));
        thread2.start(new MutexLoop(mutex));
        thread3.start(new MutexLoop(mutex));


    public void stop() {