java多线程三种方式学习笔记

java入坑系列7-java多线程三种方式学习笔记

你好! 这是关于java多线程的学习笔记。总结了3个方式实现java多线程。下面就详细总结一下。

继承Thread类

实现步骤:

  1. 定义类并继承Thread
  2. 实现run()方法
  3. 通过start()方法启动线程

例子代码:

public class TestThread extends Thread {
    public void run(){
        for (int i = 0; i < 20; i++) {
            System.out.println("run 线程:" + i);
        }
    }
    public static void main(String[] args) {

        TestThread testThread = new TestThread();
        testThread.start();

        for (int i = 0; i < 2000; i++) {
            System.out.println("主进程:" + i);
        }
    }
}

运行结果:

主进程:46
主进程:47
主进程:48
主进程:49
主进程:50
主进程:51
run 线程:0
run 线程:1
run 线程:2
run 线程:3
run 线程:4
run 线程:5
run 线程:6
run 线程:7
run 线程:8

实现Runnable接口(推荐使用此方法)

实现步骤

  1. 定义类并实现Runnable接口
  2. 实现run()方法,编写线程执行体
  3. 创建线程对象,调用start()方法启动线程
public class TestThread3 implements Runnable {
    public void run(){
        for (int i = 0; i < 20; i++) {
            System.out.println("run 线程:" + i);
        }
    }
    public static void main(String[] args) {
        TestThread3 testThread3 = new TestThread3();
        new Thread(testThread3).start();

        for (int i = 0; i < 2000; i++) {
            System.out.println("主进程:" + i);
        }
    }
}

实现Callable接口

  1. 实现Callable接口,需要返回值类型
  2. 重写call方法,需要抛出异常
  3. 创建目标对象
  4. 创建执行服务:ExecutorService ser = Executors.newFixedThreadPool(1);
  5. 提交执行:Future<Boolean> result1 = ser.submit(t1);
  6. 获取结果:boolean r1 = result1.get();
  7. 关闭服务:ser.shutdownNow();
public class TestCallable extends Callable {

    private String url;
    private String name;

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

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

    public static void main(String[] args) {
        TestCallable t1 = new TestCallable ("https://img-s-msn-com.akamaized.net/tenant/amp/entityid/BB18nvUz.img?h=174&w=300&m=6&q=60&u=t&o=t&l=f&f=jpg"
                ,"1.jpg");
        TestCallable t2= new TestCallable ("https://img-s-msn-com.akamaized.net/tenant/amp/entityid/BB196zoa.img?h=174&w=300&m=6&q=60&u=t&o=t&l=f&f=jpg"
                ,"2.jpg");
        TestCallable t3 = new TestCallable ("https://img-s-msn-com.akamaized.net/tenant/amp/entityid/BB1bO89F.img?h=174&w=300&m=6&q=60&u=t&o=t&l=f&f=jpg"
                ,"3.jpg");

	ExecutorService ser = Executors.newFixedThreadPool(1);
	Future<Boolean> r1= ser.submit(t1);
	Future<Boolean> r2= ser.submit(t2);
	Future<Boolean> r3= ser.submit(t3);

	boolean rs1 = r1.get();
	boolean rs2 = r2.get();
	boolean rs3 = r3.get();
	ser.shutdownNow(); 
    }
}
class WebDownloader {
    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");
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值