方式一:
package ThreadTest.Day1;
/**
* @author CCQ
* 创建线程的方式一:继承Thread类,重写run()方法,通过start()方法调度,
*/
public class ThreadTest01 extends Thread{
@Override
public void run() {
for (int i = 0; i < 2000; i++) {
System.out.println("线程1");
}
}
public static void main(String[] args) {
ThreadTest01 t= new ThreadTest01();
t.start();
for (int i = 0; i < 2000; i++) {
System.out.println("2");
}
}
}
package ThreadTest.Day1;
/**
* @author CCQ
* 实现多线程同步下载图片
* 用第一种方法实现多线程
*/
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class Test02 extends Thread{
private String url;
private String name;
public Test02(String url,String name) {
this.url =url;
this.name =name;
}
@Override
public void run() {
Webdowmloader webdowmloader =new Webdowmloader();
webdowmloader.dl(url,name);
System.out.println("下载了文件名为:"+name);
}
public static void main(String[] args) {
Test02 test1 =new Test02("https://huyaimg.msstatic.com/cdnimage/gamebanner/phpkKtyA71628563925.jpg","picture01.jpg");
Test02 test2 =new Test02("https://huyaimg.msstatic.com/cdnimage/gamebanner/phpMQE5MQ1628825437.jpg","picture02.jpg");
Test02 test3 =new Test02("https://huyaimg.msstatic.com/cdnimage/gamebanner/phpskpwN51594382541.jpg","picture03.jpg");
test1.start();
test2.start();
test3.start();
}
}
class Webdowmloader{
/*
FileUtils是commons库里面的一个类,里面的copyURLToFile方法可以将url传入到文件
*/
public void dl(String url,String name){
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("发生了异常!");
}
}
}
方式二:
package ThreadTest.Day1;
/**
* 实现多线程方式2:实现Runnable接口
* 创建实现类对象,将对象抛给Thread类对象调用start方法开启线程
* 推荐用这一种方式实现,因为可以实现多个接口,第一种只能单继承
*/
public class Test03 {
public static void main(String[] args) {
test t =new test();
new Thread(t).start();
for (int i = 0; i < 2000; i++) {
System.out.println("主线程");
}
}
}
class test implements Runnable{
@Override
public void run() {
for (int i = 0; i < 2000; i++) {
System.out.println("线程2");
}
}
}
package ThreadTest.Day1;
/**
* @author CCQ
* 使用第二种实现多线程方式复制图片
*/
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class test04 {
public static void main(String[] args) {
moret m1 =new moret("https://img2.baidu.com/it/u=2010459409,1898448625&fm=26&fmt=auto&gp=0.jpg","picture04.jpg");
moret m2 =new moret("https://img2.baidu.com/it/u=3821804176,575877500&fm=26&fmt=auto&gp=0.jpg","picture05.jpg");
moret m3 =new moret("https://img0.baidu.com/it/u=3950894203,3620641473&fm=26&fmt=auto&gp=0.jpg","picture06.jpg");
new Thread(m1).start();
new Thread(m2).start();
new Thread(m3).start();
}
}
class moret implements Runnable{
private String url;
private String name;
public moret(String url, String name) {
this.url = url;
this.name = name;
}
@Override
public void run() {
Webdownload webdownload =new Webdownload();
webdownload.dl(url,name);
System.out.println("下载了文件"+name);
}
}
class Webdownload{
public void dl(String url,String name){
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("出现异常了!");
}
}
}
方式三:
package ThreadTest.Day1;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.*;
/**
* 线程实现的第三种方法,实现callable接口
* 需要重写call()方法。并且需要返回值,返回值类型自己设置,但不能为基本数据类型,
*/
public class Test07 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
imt i1 =new imt("https://img2.baidu.com/it/u=2010459409,1898448625&fm=26&fmt=auto&gp=0.jpg","picture07.jpg");
imt i2 =new imt("https://img2.baidu.com/it/u=3821804176,575877500&fm=26&fmt=auto&gp=0.jpg","picture08.jpg");
imt i3 =new imt("https://img0.baidu.com/it/u=3950894203,3620641473&fm=26&fmt=auto&gp=0.jpg","picture09.jpg");
//创建执行服务。
ExecutorService service = Executors.newFixedThreadPool(3);
//提交执行。
Future<Boolean> r1 =service.submit(i1);
Future<Boolean> r2 =service.submit(i2);
Future<Boolean> r3 =service.submit(i3);
//获取结果
Boolean aBoolean = r1.get();
Boolean aBoolean1 = r2.get();
Boolean aBoolean2 = r3.get();
System.out.println(aBoolean);
System.out.println(aBoolean1);
System.out.println(aBoolean2);
; //关闭服务
service.shutdownNow();
}
}
class imt implements Callable<Boolean> {
private String url;
private String name;
public imt(String url, String name) {
this.url = url;
this.name = name;
}
@Override
public Boolean call(){
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
System.out.println("下载了文件:"+name);
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
}