//: c14:SimpleThread.java // From 'Thinking in Java, 2nd ed.' by Bruce Eckel // www.BruceEckel.com. See copyright notice in CopyRight.txt. // Very simple Threading example. //:SimpleThraed.java // // import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.util.*; import com.bruceeckel.swing.*; import com.bruceeckel.util.*; public class SimpleThread extends JApplet { JTextArea Xout = new JTextArea(10, 30); private static int threadCount = 0; public void init() { Container cp = getContentPane(); cp.setLayout(new FlowLayout()); cp.add(new JLabel("SimpleThraed")); cp.add(new JScrollPane(Xout)); for(int i = 0; i < 5; i++) new XSimpleThread().start(); Xout.append("All Threads Started\n"); } class XSimpleThread extends Thread { private int countDown = 50; private int threadNumber = ++threadCount; public XSimpleThread() { Xout.append("Making " + threadNumber + "\n"); } public void run() { while(true) { Xout.append("Thread " + threadNumber + "(" + countDown + ")\n"); if(--countDown == 0) return; } } } public static void main(String[] args) { Console.run(new SimpleThread(), 400, 250); } } ///:~