线程是程序执行流的最小单元。
Java中创建线程的方式,1)实现Ruuable接口,2)继承Thread类,3)实现Callable接口
一个例子:
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");
}
}