/********************************/ /* */ /* スレッドの作成と終了 */ /* */ /* 1998/10/ 6 宍戸 輝光 */ /* */ /********************************/ import java.awt.*; class ThreadTest extends Thread { // スレッドクラス int s; volatile public boolean go; TextField f; ThreadTest(TextField tf) { // コンストラクタ f=tf; go=true; s=0; } public void run () { // スレッドの処理 while (go) { // 終了通知を受けるまで処理を続行 f.setText(String.valueOf(s)); // 数を表示 s++; // 数を1増やす } f.setText("Thrad Stop"); // 終了時のメッセージ表示 } } public class thread1 extends java.applet.Applet { TextField no1F,no2F; Button startB,stopB; ThreadTest th1,th2; public void start(){ no1F=new TextField(8); no2F=new TextField(8); startB=new Button("Start!"); stopB=new Button("Stop!"); add(no1F); add(no2F); add(startB); add(stopB); stopB.disable(); } public void stop() { if (th1.isAlive()) th1.stop(); if (th2.isAlive()) th2.stop(); } public boolean action(Event evt,Object What) { if (evt.target==startB) { // start ボタン startB.disable(); stopB.enable(); // スレッドクラスのインスタンス作成 th1=new ThreadTest(no1F); th2=new ThreadTest(no2F); th1.start(); // スレッド起動 th2.start(); return true; } if (evt.target==stopB) { // stop ボタン startB.disable(); stopB.disable(); th1.go=false; // スレッド終了指示 th2.go=false; // スレッドが二つとも終了するまで待つ while (th1.isAlive() | th2.isAlive()); startB.enable(); return true; } return false; } }