继承Thread类,重写run方法
例如:
/**
* 实现线程方式一 继承Thread类
*
* @author wbw
*
*/
public class MyThread1 extends Thread {
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
System.out.println("Hello" + i);
Thread.sleep(10000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
启动线程
package com.my.thread;
public class Test {
public static void main(String[] args) {
new MyThread1().start();
}
}
实现Runnable接口
package com.my.thread;
/**
* 实现方式二: 实现抽象接口Runnable,在run方法中编写业务逻辑代码
* @author wbw
*
*/
public class MyThread2 implements Runnable{
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
System.out.println("Hello" + i);
Thread.sleep(10000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
在上面的两种实现方式,可以看出都能不能有返回值和不能抛出异常
在JDK1.5提供的新特性 Callable 和Fucture 可以允许线程有返回值
Callable的接口
public interface Callable<V> {
V call() throws Exception;
}
Callable和Runnable的区别:
1. Callable定义的方法是call,而Runnable定义的方法是run。
2. Callable的call方法可以有返回值,而Runnable的run方法不能有返回值。
3. Callable的call方法可抛出异常,而Runnable的run方法不能抛出异常
4. Callable的启动可以通过创建ExecutorService对象调用sumbit()方法执行,也可以通过Thread的start()方法,而Runnable通过Thread的start()方法
例子:<strong>package com.my.thread;
import java.util.concurrent.Callable;
/**
* 实现方式三:实现Callable接口
* @author wbw
*
*/
public class MyThread3 implements Callable<String> {
@Override
public String call() throws Exception {
return "hello world";
}
}</strong>
启动:
<strong>package com.my.thread;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Test {
public static void main(String[] args) {
//Callable的启动方式
MyThread3 task = new MyThread3();
ExecutorService es = Executors.newCachedThreadPool();
es.submit(task);
}
}</strong>
Future用来保存Callable异步运算的结果
FutureTask封装Future的实体类
Future的接口如下:
<strong>public interface Future<V> {
boolean cancel(boolean mayInterruptIfRunning);
boolean isCancelled();
boolean isDone();
V get() throws InterruptedException, ExecutionException;
V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
} </strong>
Callable 与Future
<strong>package com.my.thread;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
* 测试Callable
* @author wbw
*
*/
public class Test {
public static void main(String[] args) {
try {
//创建callable任务
Callable<String> task = new MyThread3();
// 创建一个执行任务的服务
ExecutorService es = Executors.newCachedThreadPool();
// 提交并执行任务,任务启动时返回了一个Future对象,
Future<String> future = es.submit(task);
// 获得第一个任务的结果,如果调用get方法,当前线程会等待任务执行完毕后才往下执行,形成阻塞的状态
System.out.println("Callable线程的返回值:"+future.get());
//停止任务执行服务
es.shutdownNow();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}</strong>
Callable 与FutureTask<strong> try {
Callable<String> task2 = new MyThread3();
//创建FutureTask对象
FutureTask<String> ft = new FutureTask<String>(task2);
Thread thread = new Thread(ft);
thread.start();//启动
Thread.sleep(100);
if(!ft.isDone()){//判断当前线程的执行状态
System.out.println(ft.get());
}
if(!ft.isCancelled()){//判断当前线程是否已被取消
ft.cancel(true);
System.out.println("Cancelled");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}</strong>
三种实现线程的方式,根据实际情况选用