继承
- 创建一个继承Thread的子类
- 重写Thread类中的run()方法 ,将线程的执行的操作声明在run()中
- 创建Thread类的子类对象
- 通过对象调用Start() :①启动当前线程 ②调用当前线程run()
说明:当我们启动一个线程,必须调用Start() ,不能调用run() 的方式启动线程
如果我们在启动一个线程,必须重新创建一个Thread子类对象,调用此对象的start()
/**
* 继承
*/
public class Thread01 {
public static void main(String[] args) {
ThreadCr threadCr = new ThreadCr();
threadCr.setName("线程一");
threadCr.start();
}
}
class ThreadCr extends Thread{
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(getName()+i);
}
}
}
实现Runnalbe接口的方式:
- 创建一个实现了Runnable接口的类
- 实现类去实现Runnable中接口的方法:run()
- 创建实现类对象
- 将此对象作为参数传递到THread类中的构造器,创建THread类的对象
- 通过Thread类的对象调用Start()
/**
* 实现
*/
public class Runnable02 {
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+i);
}
}
});
thread.setName("线程一");
thread.start();
}
}
两种方式的对比:
在开发中有优先使用的是实现Runnable接口
原因:1.实现的方式没有单继承性的局限性
2.实现的方式更适合来处理多个线程共享的数据的情况
联系:public class Thread implements Runnable
相同点:两种方式都需要重写run() ,将线程的逻辑声明在run()中。
实现Callable接口的方式
jdk5.0新增创建线程的方式
如何理解实现Callable接口的方式创建多线程比实现Runnable接口创建多线程方式强大?
-
- call()可以有返回值的。
-
- call()可以抛出异常,被外面的操作捕获,获取异常的信息
-
- Callable是支持泛型的
/**
* 实现Callable03
*/
public class Callable03 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
//3.创建Callable接口实现类的对象
NumThread thread = new NumThread();
//4.将此Callable接口实现类的对象作为传递到FutureTask构造器中,创建FutureTask的对象
FutureTask<Integer> task = new FutureTask<>(thread);
//5.将FutureTask的对象作为参数传递到Thread类的构造器中,创建Thread对象,并调用start()
new Thread(task).start();
//6.获取Callable中call方法的返回值
//get()返回值即为FutureTask构造器参数Callable实现类重写的call()的返回值
Integer integer = task.get();
System.out.println("总和为"+integer);
}
}
//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;
}
}
线程池
好处:
- 提高响应的速度(减少了创建新线程的时间)
- 降低资源消耗 (重复利用线程池中线程,不需要每次都需要创建)
- 便于线程管理
corePoolSize: 核心池的大小
maximumPoolSize:最大线程数
keepAliveTime:线程没有任时候最多保持多长时间会终止
class NumberThread1 implements Runnable{
@Override
public void run() {
for(int i = 0;i <= 100;i++){
if(i % 2 != 0){
System.out.println(Thread.currentThread().getName() + ": " + i);
}
}
}
}
public class ThreadPool {
public static void main(String[] args) {
//1. 提供指定线程数量的线程池
ExecutorService service = Executors.newFixedThreadPool(10);
ThreadPoolExecutor service1 = (ThreadPoolExecutor) service;
//设置线程池的属性
// System.out.println(service.getClass());
// service1.setCorePoolSize(15);
// service1.setKeepAliveTime();
//2.执行指定的线程的操作。需要提供实现Runnable接口或Callable接口实现类的对象
service.execute(new NumberThread());//适合适用于Runnable
service.execute(new NumberThread1());//适合适用于Runnable
// service.submit(Callable callable);//适合使用于Callable
//3.关闭连接池
service.shutdown();
}
}