线程的三种调用方法及停止线程的方法
一、线程的使用
1.使用线程的三种方法
(1)继承Thread类
继承Thread类,并覆写run()方法
public class ExtendThread extends Thread {
@Override
public void run() {
String threadName = this.getName();
System.out.println("threadName:-------" + threadName);
}
public static void main(String[] args) {
for(int n = 0; n < 5; n++) {
new ExtendThread().start();
}
}
}
(2)实现Runnable接口
实现Runnable接口并实现run()方法
public class ImplRunnable implements Runnable {
@Override
public void run() {
String threadName = Thread.currentThread().getName();
System.out.println("threadName:-------" + threadName);
}
public static void main(String[] args) {
for(int n = 0; n < 5; n++) {
new Thread(new ImplRunnable()).start();
}
}
}
(3)实现Callable接口
public class ImplCallable implements Callable<String> {
@Override
public String call() throws Exception {
String threadName = Thread.