多线程
一、创建多线程的几种方式
- 继承Thread类
- 实现runable接口
- 实现callable接口
注意:当创建一个线程后,调用的是start方法,会优先走主线程main;当调用的是run方法时,优先走run方法。线程的执行是CPU调度安排,故可能每次执行都不一样。
使用comments-io jar包进行网图图下载:
public class TestThread02 extends Thread {//通过继承Thread类
private String url;//文件路径
private String name;//文件名
public TestThread02(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) {
TestThread02 thread01 = new TestThread02("http://b397.photo.store.qq.com/psb?/V12hC8Q41MsPgM/yUU7bLy1MWF*LcU*1VAkDFBvDfj2p4xK3ZktIrbQxCQ!/b/dI0BAAAAAAAA&bo=gAJyBAAAAAAFF8A!&rf=viewer_4", "1.png");
TestThread02 thread02 = new TestThread02("http://b267.photo.store.qq.com/psb?/V12hC8Q41MsPgM/KLVrmeTeNTAexPoucX6asEYVUTDlPbkGuXn3.MkdguQ!/b/dAsBAAAAAAAA&bo=gAJyBAAAAAAFF8A!&rf=viewer_4", "2.png");
TestThread02 thread03 = new TestThread02("http://b369.photo.store.qq.com/psb?/V12hC8Q41MsPgM/sWqqYGo32OLLNBZxB5595IRZpArbTRVod*KPEQ5pCNs!/b/dHEBAAAAAAAA&bo=gAJyBAAAAAAFF8A!&rf=viewer_4", "3.png");
thread01.start();
thread02.start();
thread03.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异常,download方法出现问题");
}
}
}
public class TestCallable implements Callable<Boolean> { //通过实现Callable
private String url;//文件路径 好处:1.可以定义返回值
private String name;//文件名 2.可以抛出异常
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("http://b397.photo.store.qq.com/psb?/V12hC8Q41MsPgM/yUU7bLy1MWF*LcU*1VAkDFBvDfj2p4xK3ZktIrbQxCQ!/b/dI0BAAAAAAAA&bo=gAJyBAAAAAAFF8A!&rf=viewer_4", "1.png");
TestCallable t2 = new TestCallable("http://b267.photo.store.qq.com/psb?/V12hC8Q41MsPgM/KLVrmeTeNTAexPoucX6asEYVUTDlPbkGuXn3.MkdguQ!/b/dAsBAAAAAAAA&bo=gAJyBAAAAAAFF8A!&rf=viewer_4", "2.png");
TestCallable t3 = new TestCallable("http://b369.photo.store.qq.com/psb?/V12hC8Q41MsPgM/sWqqYGo32OLLNBZxB5595IRZpArbTRVod*KPEQ5pCNs!/b/dHEBAAAAAAAA&bo=gAJyBAAAAAAFF8A!&rf=viewer_4", "3.png");
//创建执行服务:
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异常,download方法出现问题");
}
}
}
继承Thread类和实现Runnable接口开启线程对比:
public class TestThread01 extends Thread{
@Override
public void run() {
//run方法线程体
for (int i = 0; i < 20; i++) {
System.out.println("我在玩压缩---------"+i);
}
}
//创建main方法 ,主线程
public static void main(String[] args) {
//创建一个线程对象
TestThread01 thread01 = new TestThread01();
//调用start方法开启线程
thread01.start();
for (int i = 0; i < 20; i++) {
System.out.println("我在学习---------"+i);
}
}
}
public class TestThread03 implements Runnable{
@Override
public void run() {
//run方法线程体
for (int i = 0; i < 20; i++) {
System.out.println("我在玩压缩---------"+i);
}
}
//创建main方法 ,主线程
public static void main(String[] args) {
//创建Runnable接口的实现类对象
TestThread03 testThread03 = new TestThread03();
//创建线程对象,通过线程对象开启我们的线程,代理模式
/* Thread thread = new Thread(testThread03);
thread.start();*/
new Thread(testThread03).start();
for (int i = 0; i < 20; i++) {
System.out.println("我在学习---------"+i);
}
}
}