JAVA初识多线程一之线程实现方式

方式一、继承Thread类(不建议使用)
//创建方式一:继承Thread,重写run方法,调用start开启进程

// 总结:注意:线程开启不一定立即执行,由CPU调度执行
public class TestThread extends Thread{
    @Override
    public void run() {
        for (int i=0; i < 10 ;i++){
            System.out.println("我重写了父类的run方法"+i);
        }
    }

    public static void main(String[] args) {
        TestThread testThread = new TestThread();
        testThread.start();  //开启线程

        for (int i=0; i<1000;i++){
            System.out.println("我是主线程:"+i);
        }

    }
}

多线程实践
//多线程实践
public class DownLoadThread extends Thread{
    private String imgPath;

    private String imgName;

    @Override
    public void run() {
       new downLoadImg().downLoad(imgPath,imgName);
        System.out.println("下载图片:"+imgName);
    }
    public DownLoadThread(String url, String name){
        this.imgName = name;
        this.imgPath = url;
    }
    public static void main(String[] args) {
        DownLoadThread thread1 = new DownLoadThread("https://pics1.baidu.com/feed/29381f30e924b89958a9c20b2150539d0b7bf6cf?token=eeb60e52d837786bcf41302053a10f4f&f=jpeg","1.jpg");
        DownLoadThread thread2 = new DownLoadThread("https://pics1.baidu.com/feed/ac345982b2b7d0a259ff4fc284b938014b369a1d?token=c70d71cee868911e7a66dee6a16b56ae&f=jpeg","2.jpg");
        DownLoadThread thread3 = new DownLoadThread("https://pics2.baidu.com/feed/2cf5e0fe9925bc31869a10fe1089c3b9ca13706b?token=36d26770e3253b52e617b40e5da1dfd8&f=jpeg","3.jpg");

        thread1.start();
        thread2.start();
        thread3.start();
    }

}

class downLoadImg{
    //下载器
    public void downLoad(String url, String name)  {
        try{
            URL url1 = new URL(url);
            //FileUtils方法依赖commons-io包,将网络图片(url)下载到本地
            FileUtils.copyURLToFile(url1,new File(name));
        }catch (IOException e){
            e.printStackTrace();
        }

    }
}

方式二、实现Runnable接口(建议使用)

public class RunnableThread implements Runnable{
    public void run() {
        for (int i=0; i < 10 ;i++){
            System.out.println("我重写了父类的run方法"+i);
        }
    }

    public static void main(String[] args) {
        RunnableThread testThread = new RunnableThread ();
        new Thread(testThread).start();  //开启线程

        for (int i=0; i<1000;i++){
            System.out.println("我是主线程:"+i);
        }
    }
}
方式三、实现Callable接口(了解即可)
public class CallableThread implements Callable<Object> {

    private String imgPath;

    private String imgName;

    public Object call() throws Exception {
        new downLoadImg2().downLoad(imgPath,imgName);
        System.out.println("下载图片:"+imgName);
        return null;
    }

    public CallableThread(String url, String name){
        this.imgName = name;
        this.imgPath = url;
    }
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CallableThread thread1 = new CallableThread("https://pics1.baidu.com/feed/29381f30e924b89958a9c20b2150539d0b7bf6cf?token=eeb60e52d837786bcf41302053a10f4f&f=jpeg","1.jpg");
        CallableThread thread2 = new CallableThread("https://pics1.baidu.com/feed/ac345982b2b7d0a259ff4fc284b938014b369a1d?token=c70d71cee868911e7a66dee6a16b56ae&f=jpeg","2.jpg");
        CallableThread thread3 = new CallableThread("https://pics2.baidu.com/feed/2cf5e0fe9925bc31869a10fe1089c3b9ca13706b?token=36d26770e3253b52e617b40e5da1dfd8&f=jpeg","3.jpg");
        /*
            不同之处是下面4个步骤
         */
        //1、创建执行服务
        ExecutorService service = Executors.newFixedThreadPool(3);
        //2、提交执行
        Future<Object> submit1 = service.submit(thread1);
        Future<Object> submit2 = service.submit(thread2);
        Future<Object> submit3 = service.submit(thread3);
        //3、获取结果
        Object o1 = submit1.get();
        Object o2 = submit2.get();
        Object o3 = submit3.get();
        //4、关闭服务
        service.shutdownNow();
    }
}


class downLoadImg2{
    //下載器
    public void downLoad(String url, String name)  {
        try{
            URL url1 = new URL(url);
            FileUtils.copyURLToFile(url1,new File(name));
        }catch (IOException e){
            e.printStackTrace();
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值