线程的创建方式常用有3种,继承Thread;实现Runnable;有返回的Future
一:继承Thread
public class MyThread extends Thread{
@Override
public void run() {
System.out.println("my first thread");
}
public static void main(String[] args){
MyThread myThread = new MyThread();
myThread.start();
}
}
二:实现Runnable
public class MyRunable implements Runnable {
@Override
public void run() {
System.out.println("my first Runnable");
}
public static void main(String[] args){
MyRunable myRunable = new MyRunable();
Thread thread1 = new Thread(myRunable);
Thread thread2 = new Thread(myRunable);
thread1.start();
thread2.start();
}
}
三:使用Future的子类
public class MyFuture {
public static void main(String[] args) throws Exception {
Callable ca = new Callable() {
@Override
public String call() throws Exception {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "下班";
}
};
FutureTask


