当前位置:网站首页>2. Thread creation
2. Thread creation
2022-08-09 09:32:00 【come here my bear】
线程创建
三种创建方式:
- Thread class(继承Thread类)
- 自定义线程类继承Thread类
- 重写run() 方法,编写线程执行体
- 创建线程对象,调用start() 方法启动线程
- Runnable接口(实现Runnable接口)
- 定义MyRunnable类实现Runnable接口
- 实现run()方法,编写线程执行体
- 创建线程对象,调用start()方法启动线程
- Callable接口(实现Callable)
- 重写Callable接口,需要返回值类型
- 重写call方法,需要抛出异常
- 创建目标对象
- 创建执行服务:ExecutorService ser = Executors.newFixedThreadPool(1);
- 提交执行:Future result1 = ser.sbumit(t1);
- 获取结果:boolean r1 = result1.get();
- 关闭服务:ser.shutdownNow();
推荐使用Runnable对象,因为JavaAre the limitations of single inheritance
继承Thread类
- 自定义线程类继承Thread类
- 重写run() 方法,编写线程执行体
- 创建线程对象,调用start() 方法启动线程
注:线程不一定执行,由CPU调度执行
package com.xiancheng.demo;
/**
* 创建线程方式一:
* 继承Thread类,
* 重写run()方法
* 调用start()开启线程
*/
public class TestThread extends Thread{
public static void main(String[] args) {
// main线程,主线程
// 创建一个线程对象
TestThread thread = new TestThread();
// 调用start()方法开启线程
thread.start(); // start()多条执行路径,主线程和子线程并行交替执行
// thread.run(); // run() 只有主线程一条执行路径
for (int i = 0; i < 20; i++) {
System.out.println("我在学习多线程--" + i);
}
}
// 线程入口点
@Override
public void run() {
// run() 方法线程体
for (int i = 0; i < 20; i++) {
System.out.println("我在写代码--" + i);
}
}
}
实现Runnable接口
- 定义MyRunnable类实现Runnable接口
- 实现run()方法,编写线程执行体
- 创建线程对象,调用start()方法启动线程
package com.xiancheng.demo;
// 创建线程方法
// 实现Runnable接口.重写run()方法,执行线程需要丢入runnable接口实现类,调用start()方法启动线程
public class TestThread3 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println("我在写代码");
}
}
public static void main(String[] args) {
TestThread3 testThread3 = new TestThread3();
// Thread thread = new Thread(testThread3);
// thread.start();
new Thread(testThread3).start();
for (int i = 0; i < 20; i++) {
System.out.println("我在学习多线程");
}
}
}
Callable接口
- 重写Callable接口,需要返回值类型
- 重写call方法,需要抛出异常
- 创建目标对象
- 创建执行服务:ExecutorService ser = Executors.newFixedThreadPool(1);
- 提交执行:Future result1 = ser.sbumit(t1);
- 获取结果:boolean r1 = result1.get();
- 关闭服务:ser.shutdownNow();
package com.xiancheng.demo2;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.*;
// 实现Callable接口
/*
实现Callable接口的好处:
可以有返回值
可以抛出异常
*/
public class TestCallable implements Callable<Boolean> {
private String url;
private String filename;
public TestCallable(){
}
public TestCallable(String url, String filename){
this.url = url;
this.filename = filename;
}
@Override
public Boolean call() throws Exception {
WebDownloader downloader = new WebDownloader();
downloader.downloader(url,filename);
System.out.println("下载的文件名为:" + filename);
return true;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
TestCallable callable = new TestCallable("https://tse2-mm.cn.bing.net/th/id/OIP-C.2rQ25qnSMQHXGcHz3Rp2pAHaEo?w=277&h=180&c=7&r=0&o=5&dpr=1.25&pid=1.7","2,jpg");
// 创建执行服务
ExecutorService ser = Executors.newFixedThreadPool(1);
// 提交执行
Future<Boolean> future = ser.submit(callable);
// 获取结果
System.out.println(future.get());
// 关闭服务
ser.shutdownNow();
}
}
class WebDownloader{
public void downloader(String url, String file){
try {
FileUtils.copyURLToFile(new URL(url),new File(file));
} catch (IOException e) {
e.printStackTrace();
}
}
}
小结
继承Thread类
- 子类继承Thread类具备多线程能力
- 启动线程:子类对象.start()
- 不建议使用:避免OOP单继承局限性
实现Runnable接口
- 实现接口Runnale具备多线程能力
- 启动线程:传入目标对象+Thread对象+start()
- 推荐使用:避免单继承局限性,灵活方便,方便同一个对象被多个线程使用
实现Callable接口的好处
- 可以定义返回值
- 可以抛出异常
边栏推荐
- 使用Protege4和CO-ODE工具构建OWL本体的实用指南-1.3版本(4.Building An OWL Ontology)
- unittest测试框架原理及测试流程解析,看完绝对有提升
- What are the basic concepts of performance testing?What knowledge do you need to master to perform performance testing?
- 1. Introduction to threads
- Domestic with Google earth software, see the download 19th level high-resolution satellite images so easy!
- [Machine Learning] Basics of Data Science - Basic Practice of Machine Learning (2)
- 接口开发规范及测试工具的使用
- 软件测试的流程规范有哪些?具体要怎么做?
- 1.流的概念
- [Pytorch] Install mish_cuda
猜你喜欢

Ontology Development Diary 03-Understanding Code

STM32F103实现IAP在线升级应用程序

本体开发日记03-理解代码
软件测试流程包括哪些内容?测试方法有哪些?

What are the basic concepts of performance testing?What knowledge do you need to master to perform performance testing?

Ovie map computer terminal and mobile terminal can not be used, is there any alternative map tool

Summary of steps and methods for installing and uninstalling test cases that you must read
接口测试主要测试哪方面?需要哪些技能?要怎么学习?

unittest测试框架原理及测试流程解析,看完绝对有提升
黑盒测试常见错误类型说明及解决方法有哪些?
随机推荐
本体开发日记03-排错进行时
Ontology development diary 02 - simple sparql query
GBase数据库产生迁移工具假死的原因是什么?
【个人学习总结】CRC校验原理及实现
Openwrt配置Aria2(Hg255d)
接口性能测试方案设计方法有哪些?要怎么去写?
7.FileFilter接口
4.泛型和工具类
功能自动化测试实施的原则以及方法有哪些?
6.File类
2.字节流
2. Byte stream
软件测试流程包括哪些内容?测试方法有哪些?
unix环境编程学习-多线程
银联最新测试工程师笔试题目,你能得多少分?
真·鸡汤文
oracle查看表空间占用情况并删除多余表所占空间
性能测试包括哪些方面?分类及测试方法有哪些?
本体开发日记05-努力理解SWRL(中)
单元测试是什么?怎么写?主要测试什么?