import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class firstClass implements Callable<Integer>{
//线程创建
public static void main(String[] args) throws InterruptedException, ExecutionException{
//普通方法
//方法1 Runnable
/*
* Thread t = new Thread() {
public void run() {
System.out.println("thread run ...");
}
};
t.start();*/
/* RunnableDemo r1 =new RunnableDemo("T1");
r1.start();
r1.run();
RunnableDemo r2 =new RunnableDemo("T2");
r2.start();*/
//方法2 Thread
/* ThreadDemo t1 = new ThreadDemo("T1");
t1.run();
ThreadDemo t2 = new ThreadDemo("T2");
t2.run();*/
//方法3通过 Callable 和 Future 创建线程
/*
firstClass ctt = new firstClass ();
FutureTask<Integer>ft = new FutureTask<Integer>(ctt);
for(int i = 0 ; i<10; i++) {
System.out.println(Thread.currentThread().getName()+"i:"+i);
if(i==2) {
new Thread(ft,"Thread with return value").start();
}
}
System.out.println("return value of child thread:"+ft.get());
}
@Override
public Integer call() throws Exception {
int i = 0;
for(i = 0 ;i<10;i++) {
System.out.println(Thread.currentThread().getName()+" "+i);
}
return i;*/
}
}
//方法1
/*class RunnableDemo implements Runnable{
private Thread t;
private String threadName;
public RunnableDemo(String name) {
threadName = name;
System.out.println("thread Create"+threadName );
}
public void run() {
System.out.println("thread Run"+threadName );
}
public void start() {
System.out.println("thread start"+threadName );
if(t==null) {
System.out.println("x");
t= new Thread(threadName);
t.start();
}
}*/
//方法2
/*
class ThreadDemo extends Thread{
private Thread t;
private String threadName;
public ThreadDemo(String name) {
threadName = name;
System.out.println("create"+threadName);
}*/
//重写
/*
public void run() {
System.out.println("Run"+threadName);
}
public void start() {
System.out.println("Start"+threadName);
if(t==null) {
t= new Thread(this.threadName);
t.start();
}
}
}
*/