使用Callable创建线程的步骤
1. 继承Callable接口,并实现call()方法
2. 创建FutureTask对象,包装Callable的子类对象
3. 创建Thread类的对象,包装FutureTask对象
4. 调用Thread#start()方法启动线程
5. 调用FutureTask#get()方法获取call()方法的返回值
/**
* Callable创建接口
*/
//继承Callable接口,并实现call()方法
public class MyCallable implements Callable {
public static void main(String[] args) throws InterruptedException, ExecutionException {
Callable<String> callable = new MyCallable();
FutureTask futureTask = new FutureTask(callable);
Thread thread = new Thread(futureTask,"thread1");
thread.start();
//为了让线程thread1尽快执行,让主线程休眠2秒
TimeUnit.SECONDS.sleep(2);
System.out.println(futureTask.get());
}
@Override
public Object call() throws Exception {
TimeUnit.SECONDS.sleep(5);
return new String("a")+new String("b");
}
}
/**
* Cancle实例
*/
public class MyCallableCancle implements Callable{
public static void main(String[] args) throws InterruptedException, ExecutionException {
Callable<String> callable = new MyCallable();
FutureTask futureTask = new FutureTask(callable);
Thread thread = new Thread(futureTask,"thread1");
//System.out.println(futureTask.cancel(true));
System.out.println("--------------");
thread.start();
//为了让线程thread1尽快执行,让主线程休眠2秒
TimeUnit.SECONDS.sleep(2);
System.out.println("--------------");
//只关乎在运行过程中的取消状态,参数决定cancel执行与否,与返回值无关
System.out.println(futureTask.cancel(true));
System.out.println(futureTask.get());
//线程执行之后取消 无法取消 返回false
System.out.println("--------------");
System.out.println(futureTask.cancel(true));
}
@Override
public Object call() throws Exception {
TimeUnit.SECONDS.sleep(10);
return new String("a")+new String("b");
}
}
/**
* isDone实例
*/
public class MyCallableIsDone implements Callable{
public static void main(String[] args) throws InterruptedException, ExecutionException {
Callable<String> callable = new MyCallable();
FutureTask futureTask = new FutureTask(callable);
Thread thread = new Thread(futureTask,"thread1");
System.out.println("--------------");
//线程执行前 返回false
System.out.println(futureTask.isDone());
thread.start();
//为了让线程thread1尽快执行,让主线程休眠2秒
TimeUnit.SECONDS.sleep(2);
System.out.println("--------------");
//线程执行过程中返回false
System.out.println(futureTask.isDone());
System.out.println(futureTask.get());
System.out.println("--------------");
//线程执行之后 返回true
System.out.println(futureTask.isDone());
}
@Override
public Object call() throws Exception {
TimeUnit.SECONDS.sleep(5);
return new String("a")+new String("b");
}
}
线程的生命周期
线程的生命周期就是线程从创建到运行结束的整个过程状态。
public enum State {
/**
* 线程创建但未启动
*/
NEW,
/**
* 线程处于该状态,可能是正在运行,
* 也可能是处于等待状态,在等待处理器分配资源
*/
RUNNABLE,
/**
* 线程在等待其他线程释放锁时,会处于该状态
*/
BLOCKED,
/**
* Thread state for a waiting thread.
* A thread is in the waiting state due to calling one of the
* following methods:
* <ul>
* <li>{@link Object#wait() Object.wait} with no timeout</li>
* <li>{@link #join() Thread.join} with no timeout</li>
* <li>{@link LockSupport#park() LockSupport.park}</li>
* </ul>
*
* <p>A thread in the waiting state is waiting for another thread to
* perform a particular action.
*
* For example, a thread that has called {@code Object.wait()}
* on an object is waiting for another thread to call
* {@code Object.notify()} or {@code Object.notifyAll()} on
* that object. A thread that has called {@code Thread.join()}
* is waiting for a specified thread to terminate.
*/
WAITING,
/**
* Thread state for a waiting thread with a specified waiting time.
* A thread is in the timed waiting state due to calling one of
* the following methods with a specified positive waiting time:
* <ul>
* <li>{@link #sleep Thread.sleep}</li>
* <li>{@link Object#wait(long) Object.wait} with timeout</li>
* <li>{@link #join(long) Thread.join} with timeout</li>
* <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
* <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
* </ul>
*/
TIMED_WAITING,
/**
* 线程执行完毕后处于该状态
*/
TERMINATED;
}