Threads

This document was last modified

"Concurrency" means running more than one process simultaneously.

This is the point of multi-processors and multi-processing.

In a programming language, this involves starting new "tasks" or new "threads".

For example, suppose that we want to search in three different data files (F1, F2, F3) for a particular item. We could search F1, then F2, and then F3. But, what if we could start three different threads (t1, t2, t3) each searching in one of the files. When one of them finds the item, we could stop the other two.

In practice, if not being run on a multi-processor, threads are achieved by time slicing.

Warning -- It can be much harder to test a threaded program.

Java has a class Thread which is in package java.lang (which does NOT need to be imported).

Create a new class...

class Whatever extends Thread Declare an object of that class and start() it in the Applet's init function.

You can suspend() a thread to pause it momentarily

You can resume() a suspended thread

If you stop() a thread, it is effectively dead until the applet is reloaded

You MUST write a run() method for the thread which the start() method calls in a separate execution thread

You can run the sleep(int milliseconds) method to put a thread to sleep for a while. When it awakes, it throws an InterruptedException.

import java.applet.*; import java.awt.*; import java.util.Date; public class mtclock extends Applet { Button startit = new Button("Resume"); Button stopit = new Button("Suspend"); MyThread tm; public void init() { setBackground(Color.yellow); setForeground(Color.blue); add(startit); add(stopit); tm = new MyThread(this); tm.start(); } public boolean action (Event e, Object o) { if (e.target==stopit) { tm.suspend(); return true; } else if (e.target==startit) { tm.resume(); return true; } else { return false; } } } class MyThread extends Thread { Date theDate; String previousDateText = ""; String dateText; Graphics g; Color b, f; public MyThread(Applet a) { g = a.getGraphics(); b = a.getBackground(); f = a.getForeground(); } public void paint(Graphics g) { Font font = new Font ("Helvetica", Font.BOLD, 30); g.setFont(font); g.setColor(b); g.drawString(previousDateText,10,60); g.setColor(f); g.drawString(dateText,10,60); previousDateText = dateText; } public void run() { while (true) { try { theDate = new Date(); dateText = theDate.toString(); paint(g); this.sleep(1000); } catch(InterruptedException e) { } } } } What if we had more than one Thread?

import java.applet.*; import java.awt.*; import java.util.Date; public class mtclocks extends Applet { Button suspendClock = new Button("Suspend Clock"); Button resumeClock = new Button("Resume Clock"); Button suspendGMT = new Button("Suspend GMT"); Button resumeGMT = new Button("Resume GMT"); ClockThread t1; GMThread t2; public void init() { setBackground(Color.yellow); setForeground(Color.blue); add(suspendClock); add(resumeClock); add(suspendGMT); add(resumeGMT); t1 = new ClockThread(this); t1.start(); t2 = new GMThread(this); t2.start(); } public boolean action (Event e, Object o) { if (e.target==suspendClock) { t1.suspend(); return true; } else if (e.target==suspendGMT) { t2.suspend(); return true; } else if (e.target==resumeClock) { t1.resume(); return true; } else if (e.target==resumeGMT) { t2.resume(); return true; } else { return false; } } } class ClockThread extends Thread { Date theDate; String previousDateText = ""; String dateText; Graphics g; Color b, f; public ClockThread(Applet a) { g = a.getGraphics(); b = a.getBackground(); f = a.getForeground(); } public void paint(Graphics g) { Font font = new Font ("Helvetica", Font.BOLD, 30); g.setFont(font); g.setColor(b); g.drawString(previousDateText,10,60); g.setColor(f); g.drawString(dateText,10,60); previousDateText = dateText; } public void run() { while (true) { try { theDate = new Date(); dateText = theDate.toString(); paint(g); this.sleep(1000); } catch(InterruptedException e) { } } } } class GMThread extends Thread { Date elapse; String previousDateText = ""; String dateText; Graphics g; Color b, f; public GMThread(Applet a) { g = a.getGraphics(); b = a.getBackground(); f = a.getForeground(); } public void paint(Graphics g) { Font font = new Font ("Courier", Font.BOLD, 24); g.setFont(font); g.setColor(b); g.drawString(previousDateText,10,120); g.setColor(f); g.drawString(dateText,10,120); previousDateText = dateText; } public void run() { while (true) { try { elapse = new Date(); dateText = elapse.toGMTString(); paint(g); this.sleep(5000); } catch(InterruptedException e) { } } } }