话不多说,直接看实现方式
第一种方法:继承 Thread 类
class Thread1 extends Thread{
@Override
public void run() {
for(int i = 0 ; i < 10 ;i++){
System.out.println("播放音乐"+i);
}
}
}
public class ThreadTest {
public static void main(String[] args) {
for(int i = 0 ; i < 10 ; i++){
System.out.println("玩游戏"+i);
if(i==5){
Thread1 th1 = new Thread1();
th1.start();
}
}
}
}
第二种方法:实现 Runnable 接口
class Runnable1 implements Runnable{
@Override
public void run() {
for(int i = 0 ; i < 10 ;i++){
System.out.println("播放音乐"+i);
}
}
}
public class RunnableTest {
public static void main(String[] args) {
for(int i = 0 ; i < 10 ; i++){
System.out.println("玩游戏"+i);
if(i==5){
Thread th1 = new Thread(new Runnable1());
th1.start();
}
}
}
}
第三种方法:使用匿名内部类创建线程
public static void main(String[] args) {
for(int i = 0 ; i < 10 ; i++){
System.out.println("玩游戏"+i);
if(i==5){
new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0 ; i < 10 ;i++){
System.out.println("播放音乐"+i);
}
}
}).start();
}
}
}
第四种方法:使用Callable和Future创建线程
和Runnable接口不一样,Callable接口提供了一个call()方法作为线程执行体,call()方法比run()方法功能要强大。
call()方法可以有返回值
call()方法可以声明抛出异常
Java5提供了Future接口来代表Callable接口里call()方法的返回值,并且为Future接口提供了一个实现类FutureTask,这个实现类既实现了Future接口,还实现了Runnable接口,因此可以作为Thread类的target。在Future接口里定义了几个公共方法来控制它关联的Callable任务。
boolean cancel(boolean mayInterruptIfRunning):视图取消该Future里面关联的Callable任务
V get():返回Callable里call()方法的返回值,调用这个方法会导致程序阻塞,必须等到子线程结束后才会得到返回值
V get(long timeout,TimeUnit unit):返回Callable里call()方法的返回值,最多阻塞timeout时间,经过指定时间没有返回抛出TimeoutException
boolean isDone():若Callable任务完成,返回True
boolean isCancelled():如果在Callable任务正常完成前被取消,返回True
创建步骤如下:
1】创建Callable接口的实现类,并实现call()方法,然后创建该实现类的实例(从java8开始可以直接使用Lambda表达式创建Callable对象)。
2】使用FutureTask类来包装Callable对象,该FutureTask对象封装了Callable对象的call()方法的返回值
3】使用FutureTask对象作为Thread对象的target创建并启动线程(因为FutureTask实现了Runnable接口)
4】调用FutureTask对象的get()方法来获得子线程执行结束后的返回值
① 不使用Lambda表达式
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class testCallable {
public static void main(String[] args) {
Callable<Integer> myCallable = new MyCallable(); // 创建MyCallable对象
FutureTask<Integer> ft = new FutureTask<Integer>(myCallable); // 使用FutureTask来包装MyCallable对象
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
if (i == 3) {
Thread thread = new Thread(ft); // FutureTask对象作为Thread对象的target创建新的线程
thread.start(); // 线程进入到就绪状态
}
}
System.out.println("主线程for循环执行完毕..");
try {
int sum = ft.get(); // 取得新创建的新线程中的call()方法返回的结果
System.out.println("sum = " + sum);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
class MyCallable implements Callable<Integer> {
private int i = 0;
// 与run()方法不同的是,call()方法具有返回值
@Override
public Integer call() {
int sum = 0;
for (; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
sum += i;
}
return sum;
}
}
② 使用Lambda表达式
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
public class testCallableLambda {
public static void main(String[] args) {
// 先使用Lambda表达式创建Callable<Integer>对象,
// 并使用FutureTask来包装Callable对象
FutureTask<Integer> task = new FutureTask<Integer>((Callable<Integer>) () -> {
int i = 0;
for (; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "====" + i);
}
// call()方法可以有返回值
return i;
});
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "====" + i);
if (i == 5) {
Thread t1 = new Thread(task, "有返回值的线程");
t1.start();
}
}
try {
System.out.println("子线程的返回值:" + task.get());
} catch (Exception e) {
e.printStackTrace();
}
}
}
关注码技术秘圈了解更多码技术知识