![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/fc7d85058a191fc065326b2f084148ee.png)
package com.sxt.thread;
/**
* 创建线程方式一:
* 1、创建:继承Thread+重写run
* 2、启动: 创建子类对象 + start
* @author
*
*/
public class StartThread extends Thread{
/**
* 线程入口点
*/
@Override
public void run() {
for(int i=0;i<20;i++) {
System.out.println("一边听歌");
}
}
public static void main(String[] args) {
//创建子类对象
StartThread st =new StartThread();
//启动 (启动时机)
st.start(); //不保证立即运行 cpu调用
//st.run(); //普通方法调用,单线程
for(int i=0;i<20;i++) {
System.out.println("一边coding");
}
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b8a3656df300878f2770720b0e4478f1.png)
package com.sxt.thread;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.io.FileUtils;
/**
* 下载图片
*
* @author
*
*/
public class WebDownloader {
/**
* 下载
* @param url
* @param name
*/
public void download(String url,String name) {
try {
FileUtils.copyURLToFile(new URL(url), new File(name));
} catch (MalformedURLException e) {
e.printStackTrace();
System.out.println("不合法的url");
} catch (IOException e) {
e.printStackTrace();
System.out.println("下载失败");
}
}
}
package com.sxt.thread;
public class TDownloader extends Thread {
private String url; //远程路径
private String name; //存储名字
public TDownloader(String url, String name) {
this.url = url;
this.name = name;
}
@Override
public void run() {
WebDownloader wd =new WebDownloader();
wd.download(url, name);
System.out.println(name);
}
public static void main(String[] args) {
TDownloader td1 =new TDownloader("http://upload.news.cecb2b.com/2014/0511/1399775432250.jpg","phone.jpg");
TDownloader td2 =new TDownloader("http://p1.pstatp.com/large/403c00037462ae2eee13","spl.jpg");
TDownloader td3 =new TDownloader("http://5b0988e595225.cdn.sohucs.com/images/20170830/d8b57e0dce0d4fa29bd5ef014be663d5.jpeg","success.jpg");
//启动三个线程
td1.start();
td2.start();
td3.start();
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9b5df8dd9cc33f76f7939cb0ec51aae9.png)