码迷,mamicode.com
首页 > 其他好文 > 详细

实现Runnab接口

时间:2020-06-20 18:49:01      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:nts   com   commons   开启   tps   pre   jpg   package   线程   

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接口

标签:nts   com   commons   开启   tps   pre   jpg   package   线程   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!