java多线程学习笔记---创建多线程1

方法一:Thread

继承Thread类

//创建线程方式一:
//顺序:1. 继承Thread类 2. 重写run()方法 3. 调用start开启线程
/**
 * 总结:线程开启不一定立即执行,由cpu调度
 */
public class TestThread1 extends Thread{
    @Override
    public void run() {
        //run方法线程体
        for (int i = 0; i < 20; i++) {
            System.out.println("我在好好写代码。。。"+i);
        }
    }
    //主进程
    public static void main(String[] args) {
        //main线程,主线程

        //创建一个线程对象
        TestThread1 testThread1 = new TestThread1();
        //调用start方法开启线程,注意这里开启线程的方法
        testThread1.start();
        //主线程代码
        for (int i = 0; i < 200; i++) {
            System.out.println("我在学习多线程"+i);
        }
    }
}

方法二:Runnable

实现Runnable接口

//创建线程方式:实现runnable接口,重写run方法,执行线程需要丢入runnable接口实现类,调用start方法
public class TestRunnable implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println("我在写代码"+i);
        }
    }

    public static void main(String[] args) {
        //创建runnable接口的实现类对象
        TestRunnable testRunnable = new TestRunnable();

        //创建线程对象,通过线程对象来开启我们的线程,代理
        //与Thread不同
        new Thread(testRunnable).start();
        //主线程
        for (int i = 0; i < 1000; i++) {
            System.out.println("我在写--------------"+i);
        }
    }
}

方法三:Callable

实现Callable接口,下载网络图片
与线程池有关

import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.*;

//实现线程的第三种方法,实现callable接口
public class TestCallable implements Callable<Boolean> {
    private String url;//网络图片地址
    private String name;//保存的文件名

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

    @Override
    public Boolean call() {
        WebDownloader1 webDownloader = new WebDownloader1();
        webDownloader.downloader(url,name);
        System.out.println("下载了文件名为:"+ name);
        return true;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        TestCallable testThread1 = new TestCallable("https://pic1.zhimg.com/v2-3b4fc7e3a1195a081d0259246c38debc_b.jpg","v2-3b4fc7e3a1195a081d0259246c38debc_b.jpg");
        TestCallable testThread2 = new TestCallable("http://www.chinanews.com/cul/2018/01-08/U322P4T8D8418615F107DT20180108102000.jpg","U322P4T8D8418615F107DT20180108102000.jpg");
        TestCallable testThread3 = new TestCallable("http://tb1.bdstatic.com/tb/cms/0224fangyuan-04.jpg","0224fangyuan-04.jpg");

        //创建执行服务,线程池
        ExecutorService ser = Executors.newFixedThreadPool(3);
        //提交执行
        Future<Boolean> r1 = ser.submit(testThread1);
        Future<Boolean> r2 = ser.submit(testThread2);
        Future<Boolean> r3 = ser.submit(testThread3);
        //获取结果
        Boolean rs1 = r1.get();
        Boolean rs2 = r2.get();
        Boolean rs3 = r3.get();
        //关闭服务
        ser.shutdownNow();
    }
}

//下载器
class WebDownloader1{
    //下载方法
    public void downloader(String url,String name){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常,downloader方法出现问题");
        }
    }
}

Thread实现网络照片下载

    @Override
    public void run() {
        WebDownloader webDownloader = new WebDownloader();
        webDownloader.downloader(url,name);
        System.out.println("下载了文件名为:"+ name);
    }
    public static void main(String[] args) {
        TestThread2 testThread1 = new TestThread2("https://pic1.zhimg.com/v2-3b4fc7e3a1195a081d0259246c38debc_b.jpg","v2-3b4fc7e3a1195a081d0259246c38debc_b.jpg");
        TestThread2 testThread2 = new TestThread2("http://www.chinanews.com/cul/2018/01-08/U322P4T8D8418615F107DT20180108102000.jpg","U322P4T8D8418615F107DT20180108102000.jpg");
        TestThread2 testThread3 = new TestThread2("http://tb1.bdstatic.com/tb/cms/0224fangyuan-04.jpg","0224fangyuan-04.jpg");

        //代码先下载1,再下载2,最后是3
        testThread1.start();
        testThread2.start();
        testThread3.start();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值