1. Many threads can be started in an object that implements Runnable. An alternative way to start a thread is for the object to subclass Thread but this will only one thread to be started.

import java.util.*;  

public class ThreadDemo implements Runnable { 
  protected Thread execution;  

  public synchronized void begin() { 
     if(execution == null){ 
         execution = new Thread(this); 
         execution.setPriority(Thread.MIN_PRIORITY); 
         execution.start(); 
     } 
  }  

  public synchronized void end() { 
    if(execution != null){ 
        execution.interrupy(); 
        execution = null; 
     } 
  }  

  public void run() { 
    try { 
       Thread myself = Thread.currentThread(); 
       while(execution == myself){ 
               //body 
       } 
    }finally { 
       synchronized (this){ 
       execution = null; 
    } 
  } 
}

2. A producer-consumer example. Common problem is solved by thread and semaphore.

import java.io.*; 
import java.util.*; 

public class Consumer extends Thread { 
    protected Vector objects; 

    public Consumer () { 
        objects = new Vector(); 
    } 

    public void run () { 
        try { 
             while (true) { 
                  Object object = extract (); 
                  System.out.println (object); 
             } 
        } catch (InterruptedException ignored) { } 
    } 

    protected Object extract () throws InterruptedException { 
         synchronized (objects) { 
              while (objects.isEmpty ()) { 
                  objects.wait (); 
              } 
              Object object = objects.firstElement (); 
              objects.removeElementAt (0); 
              return object; 
         } 
    } 

    public void insert (Object object) { 
         synchronized (objects) { 
              objects.addElement (object); 
              objects.notify (); 
         } 
    } 

    public static void main (String[] args) throws IOException, InterruptedException { 
        Consumer consumer = new Consumer (); 
        consumer.start (); 
        BufferedReader keyboard = new BufferedReader (new FileReader (FileDescriptor.in)); 
        String line; 
        while ((line = keyboard.readLine ()) != null) { 
            consumer.insert (line); 
            Thread.sleep (1000); 
        } 
    } 
}

3. Efficient multithreading. Recycle the threads like threads pool in JDK 5.0.

import java.util.*; 

public class ReThread implements Runnable { 
    protected Runnable target; 

    public ReThread (Runnable target) { 
        this.target = target; 
    } 

    private static int id = 0; 
    private static synchronized int getID () { return id ++; } 

    private static Vector threads = new Vector (); 
    private ReThread reThread; 
    private Thread thread; 

    public synchronized void start () { 
        if ((thread == null) && (reThread == null)) { 
              synchronized (threads) { 
                   if (threads.isEmpty ()) { 
                         thread = new Thread (this, "ReThread-" + getID ()); 
                         thread.start (); 
                   } else { 
                         reThread = (ReThread) threads.lastElement (); 
                         threads.setSize (threads.size () - 1); 
                         reThread.start0 (this); 
                   } 
              } 
        } 
    } 

    protected synchronized void start0 (ReThread reThread) { 
        this.reThread = reThread; 
        target = reThread.target; 
        notify (); 
    } 

    public synchronized void interrupt () { 
        if ((target != null) && ((thread != null) ^ (reThread != null))) { 
             if (thread != null) { 
                   thread.interrupt (); 
             } else { 
                   reThread.interrupt0 (this); 
             } 
        } 
    } 

    protected synchronized void interrupt0 (ReThread reThread) { 
        if ((target != null) && (reThread == this.reThread)) { 
              thread.interrupt (); 
        } 
    } 

    public void run () { 
        while (true) { 
            try { 
                target.run (); 
            } catch (RuntimeException ex) { 
                ex.printStackTrace (); 
            } 
            waitForTarget (); 
        } 
    } 

    protected synchronized void waitForTarget () { 
        target = null; 
        threads.addElement (this); 
        while (target == null) { 
            try { 
               wait (); 
            } catch (InterruptedException ignored) { } 
        } 
    } 
}
Advertisements