User Tools

Site Tools


technology:java:thread_example

Thread Example

This code was written in ~2004. Since then the JDK has been changed so you will need to add a package statement and located the class in the appropriate directory in your classpath. But the lesson demonstrated is multiple long-running threads.

 1: import java.lang.*;
 2: import java.text.SimpleDateFormat;
 3: import java.util.*;
 4: class threads {
 5:     public static String[] names = {
 6:     "Alice", "Bob", "Christy", "David", "Erica", "Frank", "Gary",
 7:     "Hillary", "Ian", "Jill", "Kevin", "Linda", "Mike", "Nancy", "Owen",
 8:     "Percilla", "Quinn", "Rachel", "Steve", "Tawni", "Uma", "Valerie",
 9:     "Willy", "Yeta", "Ziggy"
10:     };
11:     public static void main(String[] args) {
12:
13:     //create 10 threads and run them
14:     for (int i = 0; i < names.length; i++) {
15:         T1 t = new T1(i, names[i]);
16:         t.start();
17:     }
18:     }
19: }
20:
21: class T1
22:     extends Thread {
23:     public int threadID = 0;
24:     public SimpleDateFormat sdf = null;
25:     public String name = null;
26:     public Random rndm = null;
27:     public static String[] dailyEvents = {
28:     "alarm went off", "hit snooze button", "alarm went off again",
29:     "turned off the alarm clock and got up", "going to kitchen",
30:     "eating breakfast", "cleaning up breakfast", "heading for the shower",
31:     "taking a shower", "getting dressed", "driving to work", "working",
32:     "getting a morning snack", "working after morning snack",
33:     "going to lunch", "eating lunch", "returning from lunch",
34:     "working after lunch", "getting an afternoon snack",
35:     "ending the work day", "driving home", "eating dinner", "watching TV",
36:     "brushing teeth", "going to bed", "sleeping"
37:     };
38:     public T1(int t, String n) {
39:     threadID = t;
40:     name = n;
41:     sdf = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss.SSSS");
42:     rndm = new Random(Calendar.getInstance().getTime().getTime() +
43:               (long)t);
44:     say(name + " is alive.");
45:     }
46:
47:     public void run() {
48:     for (int x = 0; x < dailyEvents.length; x++) {
49:         say(name + " " + dailyEvents[x]);
50:         try {
51:         this.yield();
52:         this.sleep((long)(Math.random() * 100 * 120)); // 0-2 min.
53:         } catch (InterruptedException ie) {
54:         say("InterruptedException was thrown for " + name);
55:         this.destroy();
56:         }
57:     }
58:     say(name + " is terminating.");
59:     }
60:     public void say(int threadId, String msg) {
61:     System.out.println(getTime() + "Thread " + threadId + ":  " + msg);
62:     }
63:     public void say(String msg) {
64:     System.out.println(getTime() + msg);
65:     }
66:     public String getTime() {
67:     return "[" + sdf.format(new Date()) + "]";
68:     }
69: }
/home/cfreyer/public_html/data/pages/technology/java/thread_example.txt · Last modified: 2010/07/29 22:27 by Chris Freyer