Newer
Older
my90NWP / src / j1 / TestThread.java
@motoki miura motoki miura on 6 Sep 2022 944 bytes multicast chat
package j1;

//-----------------------------------------------------------
public class TestThread {
	public static void main(String[] args) {
		System.out.print("TestThread started.");

		MyThread mt1 = new MyThread("1s ", 1000);
		MyThread mt2 = new MyThread("2s ", 2000);
		MyThread mt3 = new MyThread("2.33s ", 2333);

		mt1.start();
		mt2.start();
		mt3.start();
	}
};

// -----------------------------------------------------------
class MyThread extends Thread {
	String str; // 識別用の文字列
	int msec;   // 実行間隔(ミリ秒)
	int count = 0;

	MyThread(String txt, int ms) {
		this.str = txt;
		this.msec = ms;
	}

	public void run() {
		while (true) {
			try {
				System.out.println(this.str+" (count="+count+")" );
				sleep(msec); // tミリ秒動作休止
				count++; // カウンタを1つ増やす
			} catch (InterruptedException e) {
				break;
			}
		} // end while
	} // end of run
} // end of class myThread