線程是程序執(zhí)行流的最小單元。
Java中創(chuàng)建線程的方式,1)實(shí)現(xiàn)Ruuable接口,2)繼承Thread類,3)實(shí)現(xiàn)Callable接口
一個(gè)例子:
package thread;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
public class TThread {
public static void main(String[] args) {
new Thread(new TestRunnable()).start();
new TestThread().start();
new Thread(new FutureTask<String>(new TestCallable())).start();
}
}
//第三種方式
class TestCallable implements Callable<String> {
@Override
public String call() throws Exception {
// TODO Auto-generated method stub
return "ok";
}
}
//第二種方式
class TestThread extends Thread {
@Override
public void run() {
System.out.println("Running");
}
}
//第一種方式
class TestRunnable implements Runnable {
@Override
public void run() {
System.out.println("Running");
}
}