三种创建方式
public class TestThread extends Thread{
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println("run"+i);
}
}
public static void main(String[] args) {
TestThread testThread=new TestThread();
testThread.start();
for (int i = 0; i <1000 ; i++) {
System.out.println("main"+i);
}
}
}
public class TestRunnable implements Runnable{
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println("run"+i);
}
}
public static void main(String[] args) {
TestThread testThread=new TestThread();
new Thread(testThread).start();
for (int i = 0; i <1000 ; i++) {
System.out.println("main"+i);
}
}
}
package thread;
import java.util.concurrent.*;
public class TestCallable implements Callable {
@Override
public Object call() throws Exception {
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
return true;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
TestCallable testCallable=new TestCallable();
ExecutorService service= Executors.newFixedThreadPool(1);
Future future=service.submit(testCallable);
boolean b= (boolean) future.get();
service.shutdown();
}
}