0%

多线程-Thread类

通过继承Thread类。

环境:IntelliJ IDEA、Java语言

步骤

  1. 自定义线程类继承Thread
  2. 重写run()方法,编写线程执行体
  3. 创建线程对象,调用start()方法启动线程(线程不一定立即执行,CPU安排调度)

代码演示

步骤一

自定义线程类继承Thread

1
2
3
public class TestThread1 extends Thread {

}

步骤二

重写run()方法,编写线程执行体

1
2
3
4
5
6
7
8
9
public class TestThread1 extends Thread {
@Override
public void run() {
//run方法线程体
for (int i = 0; i < 1300; i++) {
System.out.println(i+":Hello World!");
}
}
}

步骤三

创建线程对象,调用start()方法启动线程

1
2
3
4
5
6
7
8
9
10
11
12
public class Test {
public static void main(String[] args) {
//main线程,主线程
//创建一个线程对象
TestThread1 TestThread1 = new TestThread1();
//调用start()方法开启线程
TestThread1.start();
for (int i = 0; i < 3000; i++) {
System.out.println(i+":我在学习多线程--");
}
}
}

测试

通过观察程序运行的结果,我们发现并不是Hello World!打印完之后才执行main方法里的for循环,这是因为使用了多线程技术,一个程序的进程中的每个线程都是有一定的时间片轮流执行的。需要注意的是调用start()方法后线程不一定是立即执行,而是由CPU安排调度。

1
2
3
4
5
6
7
8
9
10
11
...
116:Hello World!
5:我在学习多线程--
6:我在学习多线程--
7:我在学习多线程--
8:我在学习多线程--
9:我在学习多线程--
10:我在学习多线程--
117:Hello World!
118:Hello World!
...

扩展

使用Apache基金会创建并维护的一个Java函数库–commons io包,结合多线程技术同时下载多个文件。jar包下载地址:传送门

在Java项目里新建一个包,我起的名字叫lib(library)。jar包下载好之后解压缩,将里边的commons-io-2.7.jar(我下载的版本是2.7的)文件拷贝到刚刚建立的包里。鼠标选择刚刚建立的包包,点击鼠标右键选择添加为库(Add as Library…)。

下载工具编写

创建一个下载类。命名为webDownloader。

创建对象时,提供 网络资源URL、文件保存路径、文件名(带后缀),调用start()方法使用 多线程 进行文件下载。可以只提供 网络资源URL、文件保存路径两个参数,文件名会根据URL进行自动命名(包括后缀最多20字符)。(改进:传送门)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class webDownloader extends Thread{
private String url;//网络资源URL
private String local;//下载的文件,保存路径
private String name;//下载到本地后的文件名
public webDownloader(String url,String local,String name){
this.url = url;
this.name = name;
this.local = local;
}
public webDownloader(String url,String local){
this.local = local;
this.url = url;
this.name = url.substring(url.lastIndexOf("/")+1);//若不指定文件名,则根据url进行命名,包括后缀一共不得超过20个字符
this.name = this.name.length()>20?this.name.substring(this.name.length()-20):this.name;
}
@Override
public void run() {//线程
downloader(this.url,this.local+this.name);
}
//下载方法
public void downloader(String url,String localName){
try {
File file = new File(localName);
while (file.exists()){//判断是否有重名的文件
StringBuffer newName = new StringBuffer(this.name);
newName.insert(newName.lastIndexOf("."),"0");//文件名后边加个0
this.name = newName.toString();//更新文件名
this.name = this.name.length()>20?this.name.substring(this.name.length()-20):this.name;
localName=this.local + this.name;//更新下载路径(包括文件名)localName
file = new File(localName);//更新file,下一次循环重新判断是否存在重名的文件
}
System.out.printf(this.name+"开始下载。。。\n");
FileUtils.copyURLToFile(new URL(url),new File(localName));//调用Commons io包的类,进行文件下载
System.out.printf("\n文件下载成功!保存路径为:"+localName);
} catch (IOException e) {
e.printStackTrace();
System.out.printf("\n文件下载失败!!!");
}
}
}

测试

编写测试类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class TestThread2{
public static void main(String[] args) {
//网络资源URL
String url1 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1599476670887&di=c6b212a71544515388465aa47ad81dd6&imgtype=0&src=http%3A%2F%2Fimg.mp.sohu.com%2Fq_70%2Cc_zoom%2Cw_640%2Fupload%2F20170801%2F78c2a43c2204429dbe7b864f71658f88_th.jpg";
String url2 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1599476817781&di=b5c0bb5b838466f59a47c87a67727168&imgtype=0&src=http%3A%2F%2Fattachments.gfan.com%2Fforum%2Fattachments2%2F201311%2F07%2F1336364xq9592q8kpx498q.jpg";
String url3 = "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=176574130,4070812636&fm=26&gp=0.jpg";
String url4 = "https://mirrors.tuna.tsinghua.edu.cn/apache//commons/io/binaries/commons-io-2.8.0-bin.tar.gz";
String local = "/Users/qingshuaidebaoluo/Desktop/";//下载的文件,保存路径(桌面)
webDownloader t1 = new webDownloader(url1,local);
webDownloader t2 = new webDownloader(url2,local);
webDownloader t3 = new webDownloader(url3,local,"小黄人.jpg");
webDownloader t4 = new webDownloader(url4,local,"commons-io-2.8.0-bin.tar.gz");
t1.start();
t2.start();
t3.start();
t4.start();
}
}

运行结果:

1
2
3
4
5
6
7
8
9
10
b864f71658f88_th.jpg开始下载。。。
commons-io-2.8.0-bin.tar.gz开始下载。。。
小黄人.jpg开始下载。。。
4xq9592q8kpx498q.jpg开始下载。。。

文件下载成功!保存路径为:/Users/qingshuaidebaoluo/Desktop/小黄人.jpg
文件下载成功!保存路径为:/Users/qingshuaidebaoluo/Desktop/b864f71658f88_th.jpg
文件下载成功!保存路径为:/Users/qingshuaidebaoluo/Desktop/4xq9592q8kpx498q.jpg
文件下载成功!保存路径为:/Users/qingshuaidebaoluo/Desktop/commons-io-2.8.0-bin.tar.gz
Process finished with exit code 0

学习自B站遇见狂神说

若图片不能正常显示,请在浏览器中打开

欢迎关注我的其它发布渠道