1.直接继承Thread类
创建一个继承Thread的类。
重写Thread里面的run方法。
创建子类对象,通过对象调用start(),启动线程。
//多线程实现方式1:继承Thread
class ThreadSon extends Thread{
public ThreadSon(String name) {
super(name);
}
//run方法中定义了线程需要执行的操作
@Override
public void run() {
for(int i=1;i<=100;i++) {
System.out.println(getName()+":"+i);
}
}
}
public class ThreadDemo01 {
public static void main(String[] args) {
//创建2个线程对象
Thread t1 = new ThreadSon("线程1");
Thread t2 = new ThreadSon("线程2");
/*t1.run();
t2.run();*/
//启动线程对象,线程对象启动之后,抢到cpu执行权后会调用run()执行线程操作
t1.start();
t2.start();
}
}
2.实现Runnable接口
1.创建一个实现Runnable接口的类。
2.实现Runnable里面的run方法。
3.创建实现类对象。
4.将此对象作为参数传递到Thread类的构造器中,创建Thread类的对象。
5.通过Thread类的对象调用start()。
//多线程的实现方式2:实现Runnable接口
class RunnableImpl implements Runnable{
@Override
public void run() {
for(int i=1;i<=100;i++) {
String name = Thread.currentThread().getName();//当前线程对象名
System.out.println(name+"-"+i);
}
}
}
public class ThreadDemo02 {
public static void main(String[] args) {
//线程任务
Runnable target = new RunnableImpl();
//创建2个线程对象
Thread t1 = new Thread(target,"线程1");
Thread t2 = new Thread(target,"线程2");
//启动线程对象
t1.start();
t2.start();
}
}
两种方式对比:
1.继承Thread类有很多局限性,也继承了Thread里面的其它方法。Runnable是面向接口编程。
2.Runnable更适合处理多个线程共享数据的情况。
3.Runnable实现了解耦,代码可以被多个线程共享,代码和线程独立。
4.线程池只能存放Runnable或者Callable类线程,不能直接放入继承Thread的类。
3.实现Callable接口 ----- JDK5.0新增
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
//实现多线程方式3:实现Callable接口
//1.创建一个实现Callable的实现类
class NumThread implements Callable<Integer>{
//2.实现call方法,将此线程需要执行的操作声明在call()中
@Override
public Integer call() throws Exception {
int sum = 0;
for (int i = 1; i <= 100; i++) {
if(i % 2 == 0){
System.out.println(i);
sum += i;
}
}
return sum;
}
}
public class ThreadDemo02 {
public static void main(String[] args) {
//3.创建Callable接口实现类的对象
NumThread numThread = new NumThread();
//4.将此Callable接口实现类的对象作为传递到FutureTask构造器中,创建FutureTask的对象
FutureTask futureTask = new FutureTask(numThread);
//5.将FutureTask的对象作为参数传递到Thread类的构造器中,创建Thread对象,并调用start()
new Thread(futureTask).start();
try {
//6.获取Callable中call方法的返回值
//get()返回值即为FutureTask构造器参数Callable实现类重写的call()的返回值。
Integer sum = (Integer) futureTask.get();
System.out.println("总和为:" + sum);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
4.线程池 ----JDK5.0新增
1.如果并发数量较大时,每个线程执行的时间短,频繁的创建线程会降低效率很不合理,可以通过线程池达到效果。
2.线程池其实就是一个容纳多个线程的容器,其中的线程可以反复使用,创建一个线程是一个复杂过程,对资源的也是一种浪费。
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
//多线程的实现方式4:线程池
public class ThreadDemo03 {
public static void main(String[] args) throws InterruptedException, ExecutionException {
//创建1个定长的线程池,数量为10
ThreadPoolExecutor pool = (ThreadPoolExecutor)Executors.newFixedThreadPool(10);
//提交任务,获取异步结果
Future<Integer> future = pool.submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
int sum = 0;
for(int i=1;i<=100;i++) {
if(i%2!=0) {
sum += i;
}
}
return sum;
}
});
System.out.println(future.get());
}
}
合理的使用线程池可以:
1.降低资源消耗。每个工作线程可以被重复利用,执行多个任务。
2.提高速度。当需要任务,可以不用创建线程直接执行任务。
3.提高线程的可管理性。根据系统能力,调整线程池中的数量,防止消耗过多的内存,使服务器过累。
志不强者智不达。