1.Thread类创建
通过新建thread类继承Thread父类,并重写run方法,其中使用common-io的jar包实现一个下载器,主函数中创建3个线程,并用start函数执行。
public class thread extends Thread{
private String url;
private String name;
public thread(String url,String name){
this.url = url;
this.name = name;
}
@Override
public void run() {
WebDownloader wd = new WebDownloader();
wd.downloader(url,name);
System.out.println("name:"+name);
}
public static void main(String[] args) {
thread t1 = new thread("https://exp-picture.cdn.bcebos.com/16a84fe10ef85856d946959753e9ccd2ba66cdcd.jpg","图片1.jpg");
thread t2 = new thread("https://exp-picture.cdn.bcebos.com/16a84fe10ef85856d946959753e9ccd2ba66cdcd.jpg","图片2.jpg");
thread t3 = new thread("https://exp-picture.cdn.bcebos.com/16a84fe10ef85856d946959753e9ccd2ba66cdcd.jpg","图片3.jpg");
t1.start();
t2.start();
t3.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异常");
}
}
}
结果如下,可以看出是并行执行的。
2.Runnable接口实现
同理,runnable接口实际上是使用new Thread(Runnable实例).start()来实现的。
public class runnable implements Runnable{
private String url;
private String name;
public runnable(String url,String name){
this.url = url;
this.name = name;
}
@Override
public void run() {
WebDownloader1 wd = new WebDownloader1();
wd.downloader(url,name);
System.out.println("name:"+name);
}
public static void main(String[] args) {
runnable t1 = new runnable("https://exp-picture.cdn.bcebos.com/16a84fe10ef85856d946959753e9ccd2ba66cdcd.jpg","图片1.jpg");
runnable t2 = new runnable("https://exp-picture.cdn.bcebos.com/16a84fe10ef85856d946959753e9ccd2ba66cdcd.jpg","图片2.jpg");
runnable t3 = new runnable("https://exp-picture.cdn.bcebos.com/16a84fe10ef85856d946959753e9ccd2ba66cdcd.jpg","图片3.jpg");
new Thread(t1).start();
new Thread(t2).start();
new Thread(t3).start();
}
}
class WebDownloader1{
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异常");
}
}
}
3.Callable实现多线程
public class call implements Callable<Boolean> {
private String url;
private String name;
public call(String url, String name){
this.url = url;
this.name = name;
}
@Override
public Boolean call() {
WebDownloader wd = new WebDownloader();
wd.downloader(url,name);
System.out.println("name:"+name);
return true;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
call t1 = new call("https://exp-picture.cdn.bcebos.com/16a84fe10ef85856d946959753e9ccd2ba66cdcd.jpg","图片1.jpg");
call t2 = new call("https://exp-picture.cdn.bcebos.com/16a84fe10ef85856d946959753e9ccd2ba66cdcd.jpg","图片2.jpg");
call t3 = new call("https://exp-picture.cdn.bcebos.com/16a84fe10ef85856d946959753e9ccd2ba66cdcd.jpg","图片3.jpg");
//创建服务
ExecutorService es = Executors.newFixedThreadPool(3);
Future<Boolean> r1 = es.submit(t1);
Future<Boolean> r2 = es.submit(t2);
Future<Boolean> r3 = es.submit(t3);
boolean rs1 = r1.get();
boolean rs2 = r2.get();
boolean rs3 = r3.get();
es.shutdown();
}
}
Callable方法主要是借助创建一个服务来提交实现多线程。