SE450: Thread Example [7/36] Previous pageContentsNext page

file:horstmann/ch09_greeting/GreetingProducer.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package horstmann.ch09_greeting;
/**
   An action that repeatedly prints a greeting.
 */
public class GreetingProducer implements Runnable
{
  /**
      Constructs the producer object.
      @param aGreeting the greeting to display
   */
  public GreetingProducer(String aGreeting)
  {
    greeting = aGreeting;
  }

  public void run()
  {
    try
    {
      for (int i = 1; i <= REPETITIONS; i++)
      {
        System.out.println(i + ": " + greeting);
        Thread.sleep(DELAY);
      }
    }
    catch (InterruptedException exception)
    {
    }
  }

  private String greeting;

  private static final int REPETITIONS = 10;
  private static final int DELAY = 100;
}

file:horstmann/ch09_greeting/ThreadTester.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
package horstmann.ch09_greeting;
/**
   This program runs two threads in parallel.
 */
public class ThreadTester
{
  public static void main(String[] args)
  {
    Runnable r1 = new GreetingProducer("Hello, World!");
    Runnable r2 = new GreetingProducer("Goodbye, World!");

    Thread t1 = new Thread(r1);
    Thread t2 = new Thread(r2);

    t1.start();
    t2.start();
  }
}

.

Previous pageContentsNext page