线程的创建方式 --狂神说

一 继承Thread类

1.案例一

两个线程交替打印

public class ThreadTest01 extends Thread{

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

    public static void main(String[] args) {
        ThreadTest01 t=new ThreadTest01();
        t.start();
        for(int i=0;i<2000;i++){
            System.out.println("主线程执行了----"+i);
        }
    }
}

2. 案例二:多线程下载文件

import org.apache.commons.io.FileUtils;

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

public class ThreadTest02 extends Thread {
    String url;
    String filename;
    public ThreadTest02(String url, String filename) {
        this.url = url;
        this.filename=filename;
    }

    @Override
    public void run() {
        FileDownload.download(url,filename);
        System.out.println(filename+"下载完成");
    }

    public static void main(String[] args) {
        ThreadTest02 t1=new ThreadTest02("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1594358850053&di=60adf4b0d50993204c457e85ac6a1d26&imgtype=0&src=http%3A%2F%2Fp1.qhimgs4.com%2Ft01a30c675c53e713c2.jpg","01.jpg");
        ThreadTest02 t2=new ThreadTest02("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1594358850048&di=8d6591071db4c1b63b041f5fc76cb1e8&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201804%2F15%2F20180415191938_ufvFv.jpeg","02.jpg");
        ThreadTest02 t3=new ThreadTest02("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1594358850051&di=0e55cff215324a089df6744714074e74&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201710%2F02%2F20171002115624_vYmNs.jpeg","03.jpg");
        t1.start();
        t2.start();
        t3.start();
    }
}

class FileDownload{
    public static void download(String url,String filename){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(filename));
        } catch (IOException e) {
            System.out.println("文件下载失败");
        }
    }
}

二 实现Runnable接口

1. 案例一,两个线程交替打印

public class ThreadTest03 {
    public static void main(String[] args) {
        //创建分支线程对象
        MyRunnable myRunnable=new MyRunnable();
        Thread t=new Thread(myRunnable);
        //启动分支线程
        t.start();
        for(int i=0;i<2000;i++){
            System.out.println("主线程执行了----"+i);
        }

    }
}
class MyRunnable implements Runnable{
    public void run() {
        for(int i=0;i<2000;i++){
            System.out.println("分支线程执行了----"+i);
        }
    }
}

三 实现Callable接口

案例:下载文件

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class ThreadTest05 {
    public static void main(String[] args) {
        MyCallable callable=new MyCallable("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1594358850053&di=60adf4b0d50993204c457e85ac6a1d26&imgtype=0&src=http%3A%2F%2Fp1.qhimgs4.com%2Ft01a30c675c53e713c2.jpg","01.jpg");
        FutureTask ft=new FutureTask(callable);
        Thread t=new Thread(ft);
        t.start();
        System.out.println("主线程执行完毕");
    }
}

class MyCallable implements Callable{
    String url;
    String filename;
    public MyCallable(String url, String filename) {
        this.url = url;
        this.filename = filename;
    }
    public Boolean call() throws Exception {
        FileDownloader.download(url,filename);
        System.out.println("图片下载完成");
        return true;
    }
}

class FileDownloader{
    public static void download(String url,String filename){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(filename));
        } catch (IOException e) {
            System.out.println("文件下载失败");
        }
    }
}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值