CS 4773 Object Oriented Systems


The Java Language (continued)


Class Modifiers

abstract final public private < empty >

Method and Field Modifiers

public protected private < empty > final static synchronized native

Threads

There are two main ways to use threads in Java:

Ping Pong Application

Code for the PingPong programs can be found in
/usr/local/courses/cs4773/spring97/threadtests

PingPong.java

package threadtests;
public class PingPong extends Thread {
   String word;                 // what word to print
   int delay;                   // how long to pause
   int count;                   // number of iterations

   PingPong(String What, int Time, int number) {
      word = What;
      delay = Time;
      count = number;
      setName(What);
   }
   public void run() {
      try {
         for(int i=0;i < count;i++) {
            System.out.println(i+": "+word);
            sleep(delay);    // wait until next time
         }
      }  catch (InterruptedException e) {
         return;                 // end this thread
      }  
   }   
}

test1.java

package threadtests;
class test1{
  public static void main (String[] args){
     PingPong ping;
     PingPong pong;
     ping = new PingPong("ping", 1000, 20);
     pong = new PingPong("PONG", 3000, 8);
     ping.start();
     pong.start();
  }
}

Ping Pong Output

0: ping
0: PONG
1: ping
2: ping
1: PONG
3: ping
4: ping
5: ping
2: PONG
6: ping
7: ping
8: ping
3: PONG
9: ping
10: ping
4: PONG
11: ping
12: ping
13: ping
5: PONG
14: ping
15: ping
16: ping
6: PONG
17: ping
18: ping
7: PONG
19: ping
8: PONG
9: PONG
10: PONG
11: PONG
12: PONG

Using join

Join suspends the caller until the thread has completed.
package threadtests;
class test2{

   public static void show_threads(String msg) {
      Thread[] tlist = new Thread[50];
      int count;
      count = Thread.enumerate(tlist);
      System.out.println(msg + " Number of threads: "+count);
      for (int i=0;i < count;i++) 
        System.out.println(i+": "+tlist[i]);
   }

  public static void main (String[] args){
     PingPong ping;
     PingPong pong;

     show_threads("Start of main");
     ping = new PingPong("ping", 1000, 20);
     show_threads("ping created");
     pong = new PingPong("PONG", 3000, 8);
     show_threads("pong created");
     ping.start();
     pong.start();
     try {ping.join();} catch(InterruptedException e) {}
     show_threads("ping joined");
     try {pong.join();} catch(InterruptedException e) {}
     show_threads("pong joined");
  }
}

Join Ping Pong Output

Start of main Number of threads: 1
0: Thread[main,5,main]
ping created Number of threads: 2
0: Thread[main,5,main]
1: Thread[ping,5,main]
pong created Number of threads: 3
0: Thread[main,5,main]
1: Thread[ping,5,main]
2: Thread[PONG,5,main]
0: ping
0: PONG
1: ping
2: ping
1: PONG
3: ping
4: ping
5: ping
2: PONG
6: ping
7: ping
8: ping
3: PONG
9: ping
10: ping
11: ping
4: PONG
12: ping
13: ping
14: ping
5: PONG
15: ping
16: ping
17: ping
6: PONG
18: ping
19: ping
ping joined Number of threads: 2
0: Thread[main,5,main]
1: Thread[PONG,5,main]
7: PONG
pong joined Number of threads: 1
0: Thread[main,5,main]