1.实现Runnable接口
- 使用Runnable实现多线程,需要一个类实现Runnable接口,并实现Runnable接口的run方法。
- Runnable线程实例启动需要创建Thread实例 自身作为参数传递,然后调用Thread的start()方法。
示例如下:
public class Person implements Runnable {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + " 遇到堵车了。。。");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + " is coming");
}
}
class TestThread {
public static void main(String[] args) {
Person person1 = new Person();
Person person2 = new Person();
Person person3 = new Person();
new Thread(person1, "张三").start();
new Thread(person2, "李四").start();
new Thread(person3, "王五").start();
}
}
jdk8的写法, 由于Runnable接口是一个函数式接口,可以使用lambda表达式
new Thread(() -> {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + " 遇到堵车了。。。");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + " is coming");
}, "张三").start();
2.继承Thread类
Thread类本身也实现了Runable接口
public class Thread
extends [Object]
implements [Runnable]
- 使用Thread类实现多线程, 需要将一个类声明为Thread的子类, 这个子类应该重写Thread类的run方法。
- Thread线程实例启动使用start()方法
示例如下:
public class Person extends Thread {
/**
* 人名 / 线程名
*/
private String name;
Person(String name) {
super(name);
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + " 遇到堵车了。。。");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + " is coming");
}
}
class TestThread {
public static void main(String[] args) {
Person person1 = new Person("张三");
Person person2 = new Person("李四");
Person person3 = new Person("王五");
person1.start();
person2.start();
person3.start();
}
}
3.实现Callable接口
java.util.concurrent
Interface Callable
Callable接口与Runnable接口类似,实例是给线程执行的类作为参数
3.1.Callable接口与Runnable接口区别
1.实现Callable接口,要实现call()方法;
2.call()方法会有一个返回值 会抛出异常;
3.2.Callable如何获区返回值
当call()方法完成时,结果必须存储在主线程已知的对象中,以便主线程可以知道该线程返回的结果, 为此,java提供了Future<V>
接口
public interface Future<V> {
// 用于停止任务。如果尚未启动,它将停止任务。如果已启动,则仅在mayInterrupt为true时才会中断任务。
boolean cancel(boolean mayInterruptIfRunning);
// Returns {@code true} if this task was cancelled before it completed
boolean isCancelled();
// Returns {@code true} if this task completed
boolean isDone();
// Waits if necessary for the computation to complete, and then retrieves its result.
V get() throws InterruptedException, ExecutionException;
// Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.
V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}
RunnableFuture
接口继承了Runnable
和 Future
接口
FutureTask
实现了 RunnableFuture
接口
所以FutureTask
既能接收call()方法的返回值,又可以提供给Thread的构造方法创建线程。
FutureTask
提供了参数为Callable的构造方法 FutureTask(Callable<V> callable)
public interface RunnableFuture<V> extends Runnable, Future<V>
public class FutureTask<V> implements RunnableFuture<V>{
public FutureTask(Callable<V> callable) {
if (callable == null)
throw new NullPointerException();
this.callable = callable;
this.state = NEW; // ensure visibility of callable
}
}
示例如下:
/**
* 测试使用Callable 创建线程
*/
public class CallableTest {
public static void main(String[] args) {
FutureTask<Object> task1 = new FutureTask<>(new MyCallable());
FutureTask<Object> task2 = new FutureTask<>(new MyCallable());
FutureTask<Object> task3 = new FutureTask<>(new MyCallable());
Thread t1 = new Thread(task1, "person1");
Thread t2 = new Thread(task2, "person2");
Thread t3 = new Thread(task3, "person3");
t1.start();
t2.start();
t3.start();
// 获取线程返回值
try {
System.out.println("task1:" + task1.get());
System.out.println("task2:" + task2.get());
System.out.println("task3:" + task3.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
/**
* 实现接口 Callable 并实现方法call()
*/
class MyCallable implements Callable<Object> {
@Override
public Object call() throws Exception {
for (int i = 0; i < 5; i++) {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + " 遇到堵车了。。。");
}
return Thread.currentThread().getName() + " SUCCESS";
}
}