1.继承Thread类
优点:代码简单,可以直接使用Thread类里面的方法。
缺点:扩张性较差,应为在java中,一个类只能继承一个父类。
2.实现Runnable接口
3.实现Callable接口
2和3的优缺点是一样的
优点:扩展性强,实现该接口的同时还可以继承其他类。
缺点:代码相对来说要复杂一点,并且不能直接使用Thread类里面的方法
1.继承Thread类
package com.java.thread;
public class ThreadDemo01 {
public static void main(String[] args) {
/*
多线程的第一种实现方式是继承Thread类
1.自己创建一个类来继承Thread类
2.重写run方法
3.创建子类对象并启动线程
*/
int num = 5;//井水的初始数量
//创建子类对象,实例化子类对象
Well oldwellZbj = new Well(num,"---猪八戒---");
Well oldwellSwk = new Well(num,"***孙悟空***");
//我们不使用oldwellzbj.run,因为这不是开启线程的方法
//开启线程
oldwellZbj.start();
oldwellSwk.start();
for (int i = 0; i <= num; i++) {
System.out.println("当前线程:" + Thread.currentThread().getName());
try {
Thread.sleep(2000);//当前线程休眠2秒
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
package com.java.thread;
//创建一个类并继承Thread类
public class Well extends java.lang.Thread {
private int num;//井水的数量
public Well(int num, String name) {
super(name);
this.num = num;//初始化数量
}
//重写run方法,实现取水的任务
public void run() {
for (int i = 0; i <this.num; i++) {
System.out.println(this.getName() + "取出第" + i + "桶水" + ",剩余水量:" + (this.num - i) + "桶");
try {
Thread.sleep(2000);//线程休眠2秒
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
2.实现Runnable接口
package com.java.thread;
//测试类
public class RunnableDemo01 {
public static void main(String[] args) {
/*
1. 定义一个类实现Runnable接口
2.利用Runnable接口的子类对象作为Thread构造方法的参数创建Thread对象
3.调用Thread对象的start方法启动线程
*/
//创建对象,我们要创建2个对象,一个是我们自己创建的类的对象,一个是Thread的对象
//自己这个类表示多线程要操作的内容
Runnable01 oldwell = new Runnable01(5);//井水有5桶
//Thread类这个对象的创建是为了创建线程的对象
Thread swk = new Thread(oldwell,"---孙悟空---");
Thread zbj = new Thread(oldwell,"***猪八戒***");
//调用线程的start方法
swk.start();
zbj.start();
}
}
package com.java.thread;
//重新定义一个类实现Runnable接口
public class Runnable01 implements Runnable {
private int num;//井水数量
public Runnable01(int num) {
this.num = num;
}
@Override
public void run() {
while (true){
if(num > 0){//表示水井有水
System.out.println(Thread.currentThread().getName() + "开始取水\n");
System.out.println("取出1桶水," + "剩余水量" + (--num) + "桶");
}else{//表示水井里面没水了
break;//退出循环
}
try {
Thread.sleep(500);//线程休眠0.5秒
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
3.实现Callable接口
package com.java.thread;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class CaseDemo03 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
/*
特点:可以获取多线程的运行结果
1.创建一个类来实现Callable接口
2.重写Callable里面的call方法(是有返回值的,表示多线程的运行结果
3.创建自己创建的那个类的对象(表示多线程要执行的任务
4.创建FutureTask的对象(管理多线程的运行结果)
(Future是一个接口,我们要创建FutureTask这个Future的实现方法的对象)
5.创建Thread类的对象,并启动线程(表示线程的启动)
*/
//创建自己创建的那个类的对象(表示多线程要执行的任务
Case03 c = new Case03();
//创建FutureTask的对象
FutureTask<Integer> ft = new FutureTask<>(c);//表示用FutureTask这个对象去管理c的结果
// 创建Thread类的对象,并启动线程(表示线程的启动)
new Thread(ft).start();
//获取多线程的运行结果
int result = ft.get();
System.out.println(result);
}
}
package com.java.thread;
import java.util.concurrent.Callable;
public class Case03 implements Callable<Integer> {
//这里的Integer表示泛型,表示多线程运行的结果
@Override
public Integer call() throws Exception {
//求1~100之间的和
int sum = 0;
for (int i = 0; i <= 100; i++) {
sum += i;
}
return sum;
}
}
非常感谢你可以看到这里!!!