java入門–スレッド(Thread)の実装方法まとめ

方法1
public class ZtThread extends Thread {
String name;
public ZtThread(String name){
this.name = name;
}
public void run(){
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {

e.printStackTrace();
}
System.out.println(name+”loading”);
}
}
}

スレッド起動:
public static void main(String[] args) {
ZtThread z1 = new ZtThread(“thread1 “);
z1.start();
ZtThread z2 = new ZtThread(“thread2”);
z2.start();

方法2
public class TestThread extends OtherClass implements Runnable {
public void run() {
System.out.println(“TestThread.run()”);
}
}

TestThread mmThread = new TestThread();
Thread thread = new Thread(mmThread);
thread.start();