1.基本介绍
2.线程创建
方式1:
package thread_;
//创建线程方式一:继承Thread类,重写run()方法,调用start开启线程
public class thread01 extends Thread {
@Override
public void run() {
//run方法线程体
for (int i = 0; i < 20; i++) {
System.out.println("我在学习java" + i);
}
}
public static void main(String[] args) {
//main线程,主线程
thread01 thread01 = new thread01();//创建一个多线程
thread01.start();//开启线程
for (int i = 0; i < 20; i++) {
System.out.println("我在学习spring" + i);
}
}
}
使用多线程下载三张网图:
package thread_;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
//联系thread,实现多线程同步下载图片
public class thread2 extends Thread {
private String url;
private String name;
public thread2(String url,String name){
this.url = url;
this.name = name;
}
//下载图片线程的执行体
@Override
public void run() {
webDownloader webDownloader = new webDownloader();
webDownloader.downloader(url,name);
System.out.println("下载了文件名为" + name);
}
public static void main(String[] args) {
thread2 thread2 = new thread2("https://img-blog.csdnimg.cn/10d936d490d745e6bee95d25cbe5acbd.png","照片1");
thread2 thread3 = new thread2("https://img-blog.csdnimg.cn/10d936d490d745e6bee95d25cbe5acbd.png","照片2");
thread2 thread4 = new thread2("https://img-blog.csdnimg.cn/10d936d490d745e6bee95d25cbe5acbd.png","照片3");
thread2.start();
thread3.start();
thread4.start();
}
}
//下载器
class webDownloader {
//下载方法
public void downloader(String url, String name) {
try {
FileUtils.copyURLToFile(new URL(url), new File(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO异常,downloader方法出现异常");
}
}
}
方式2:
package thread_;
//创建线程方法2:实现runnable接口,重写run方法,调用start()
public class thread03 implements Runnable {
@Override
public void run() {
//run方法线程体
for (int i = 0; i < 2; i++) {
System.out.println("我在打篮球-----");
}
}
public static void main(String[] args) {
//创建一个runnable接口的实现类对象
thread03 thread03 = new thread03();
//创建线程对象,通过线程对象来开启我们的线程
new Thread(thread03).start();
for (int i = 0; i < 1; i++) {
System.out.println("我在学习java");
}
}
}
方式3:
package thread_.Callable;
import org.apache.commons.io.FileUtils;
import thread_.thread2;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.*;
/**
* 线程创建方式三,实现callable接口
* callable的好处
* 1.可以定义返回值
* 2.可以抛出异常
*
*/
public class TestCallable implements Callable<Boolean> {
private String url;
private String name;
public TestCallable(String url, String name) {
this.url = url;
this.name = name;
}
//下载图片线程的执行体
@Override
public Boolean call() {
webDownloader webDownloader = new webDownloader();
webDownloader.downloader(url, name);
System.out.println("下载了文件名为" + name);
return true;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
TestCallable t1 = new TestCallable("https://img-blog.csdnimg.cn/10d936d490d745e6bee95d25cbe5acbd.png", "照片1");
TestCallable t2 = new TestCallable("https://img-blog.csdnimg.cn/10d936d490d745e6bee95d25cbe5acbd.png", "照片2");
TestCallable t3 = new TestCallable("https://img-blog.csdnimg.cn/10d936d490d745e6bee95d25cbe5acbd.png", "照片3");
//创建执行服务
ExecutorService ser = Executors.newFixedThreadPool(3);
//提交执行
Future<Boolean> r1 = ser.submit(t1);
Future<Boolean> r2 = ser.submit(t2);
Future<Boolean> r3 = ser.submit(t3);
//获取结果
boolean rs1 = r1.get();
boolean rs2 = r2.get();
boolean rs3 = r3.get();
//关闭服务
ser.shutdownNow();
}
}
//下载器
class webDownloader {
//下载方法
public void downloader(String url, String name) {
try {
FileUtils.copyURLToFile(new URL(url), new File(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO异常,downloader方法出现异常");
}
}
}