首页 > 其他 > 详细

实现Runnab接口

时间:2020-06-20 18:16:36      阅读:52      评论:0      收藏:0      [点我收藏+]

Runnable

package duoxiancheng;

public class Thread2 implements Runnable{


    @Override
    public void run() {
        for (int i = 0; i < 200; i++) {
            System.out.println("线程执行");
        }
    }



    public static void main(String[] args) {
        //创建runnable接口的实例对象
        Thread2 thread2 =new Thread2();
        //创建线程对象,通过线程对象开启线程,代理
        Thread thread = new Thread(thread2);
        thread.start();

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



}

多线程下载

package duoxiancheng;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

public class Thread2 implements Runnable{

    String url;
    String name;
    public Thread2(String url,String name){
        this.url=url;
        this.name=name;
    }

    @Override
    public void run() {
        Downnn downnn=new Downnn();
        downnn.dodd(url,name);
        System.out.println("下载了:"+name);
    }



    public static void main(String[] args) {
        Thread2 thread1 = new Thread2("https://n.sinaimg.cn/fashion/590/w240h350/20200618/5242-ivffpcs0137589.jpg","1.jpg");
        Thread2 thread2 = new Thread2("https://n.sinaimg.cn/fashion/590/w240h350/20200618/5242-ivffpcs0137589.jpg","2.jpg");
        Thread2 thread3 = new Thread2("https://n.sinaimg.cn/fashion/590/w240h350/20200618/5242-ivffpcs0137589.jpg","3.jpg");
        new Thread(thread1).start();
        new Thread(thread2).start();
        new Thread(thread3).start();
    }

    class Downnn{
        public void dodd(String url,String name){
            try {
                FileUtils.copyURLToFile(new URL(url),new File(name));
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    }


}

实现Runnab接口

原文:https://www.cnblogs.com/itzyz/p/13169805.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!