Java多线程的三种实现方法

核心概念

obsidian://open?vault=Notes&file=Java%2FPasted%20image%2020220323192030.png

线程的创建

三种创建方式:

  1. 继承Thread类(重点)
  2. 实现Runnable接口(重点)
  3. 实现Callable接口(现阶段了解)

Thread

  • 自定义线程类继承Thread类
  • 重写run()方法,编写线程执行体
  • 创建线程对象,调用start()方法启动线程
//自定义线程类继承Thread类
public class TestThread extends Thread{

//重写run()方法,编写线程执行体
    @Override  
 public void run() {  
        for (int i = 0; i < 200; i++) {  
            System.out.println("我在学习多线程--"+i);  
 }  
    }  
    public static void main(String[] args) {
    //创建线程对象,调用start()方法启动线程  
        new TestThread().start();  
 for (int i = 0; i < 200; i++) {  
            System.out.println("我在吃饭--"+i);  
 }  
    }  
}

运行结果:

我在吃饭--0
我在学习多线程--0
我在吃饭--1
我在学习多线程--1
我在吃饭--2
我在学习多线程--2
我在吃饭--3
……
package multithreading;  
  
import org.apache.commons.io.FileUtils;  
  
import java.io.File;  
import java.io.IOException;  
import java.net.URL;  
  
public class TestDownloads extends Thread{  
    private String url;  
 private String name;  
 public TestDownloads(String url,String name){  
        this.url = url;  
 this.name = name;  
 }  
    public void run(){  
        PictureDownloader imageDownload = new PictureDownloader();  
 imageDownload.download(url,name);  
 }  
  
    public static void main(String[] args) {  
        TestDownloads t1 = new TestDownloads("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.win4000.com%2Fwallpaper%2F9%2F55768db3ccdcf_120_80.jpg&refer=http%3A%2F%2Fpic1.win4000.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1650629529&t=bb51364bb90c562bdb8ec56b80a6ee34","1.jpg");  
 TestDownloads t2 = new TestDownloads("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.win4000.com%2Fmobile%2F8%2F59c4d3f159f23_130_170.jpg&refer=http%3A%2F%2Fpic1.win4000.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1650629529&t=b5026057ca97c05914774a55eff3808f","2.jpg");  
 TestDownloads t3 = new TestDownloads("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.win4000.com%2Fmobile%2F2018-01-04%2F5a4d87025a2b2_130_170.jpg&refer=http%3A%2F%2Fpic1.win4000.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1650629529&t=f410fa5910311fdcf48427969c96b7d5","3.jpg");  
 t1.start();  
 t2.start();  
 t3.start();  
 }  
}  
// 图片下载器  
class PictureDownloader  
{  
    public void download(String url,String name){  
        try {  
            FileUtils.copyURLToFile(new URL(url),new File(name));  
 System.out.println("图片下载完成:"+name);  
 } catch (IOException e) {  
            System.out.println("IO下载器出现异常,downloadsImage");  
 }  
    }  
}

执行结果:

图片下载完成:3.jpg
图片下载完成:1.jpg
图片下载完成:2.jpg

Runnable

  • 定义MyRunnable类实现Runnable接口
  • 实现run()方法,编写线程执行体
  • 创建线程对象,调用start()方法启动线程
package multithreading;  
//定义MyRunnable类实现Runnable接口
public class TestRunnable implements Runnable{
//实现run()方法,编写线程执行体
    @Override  
 public void run() {  
        for (int i = 0; i < 200; i++) {  
            System.out.println("我在学习Runnable接口多线程--"+i);  
 }  
    }  
  
    public static void main(String[] args) {  
        //创建Runnable接口的实现类对象  
 TestRunnable r = new TestRunnable();  
 //创建线程对象,通过对象来开启线程,代理  
 Thread thread = new Thread(r);// 里面的参数是Runnable接口的实现类  
 //这里可以用匿名创建对象的方式
 thread.start();  
 for (int i = 0; i < 200; i++) {  
            System.out.println("我在吃饭--"+i);  
 }  
    }  
}

结果:

我在学习Runnable接口多线程--0
我在吃饭--0
我在学习Runnable接口多线程--1
我在吃饭--1
我在学习Runnable接口多线程--2
我在吃饭--2
我在学习Runnable接口多线程--3
我在吃饭--3
我在学习Runnable接口多线程--4
我在吃饭--4
……

小结

  • 继承Thread类
    • 子类继承Thread类具备多线程能力
    • 启动线程:子类对象.start()
    • 不建议使用:避免OOP单继承局限性
  • 实现Runnable接口
    • 实现接口Runnable具有多线程能力
    • 启动线程:传入目标对象+Thread对象.start()
    • 推荐使用:避免单继承局限性,灵活方便,方便同一个对象被多个线程使用。

Callable

  • 实现Callable接口,需要返回值类型
  • 重写call方法,需要抛出异常
  • 创建目标对象
  • 创建执行服务:ExecutorService ser = Executors.newFixedThreadPool(1);
  • 提交执行:Future result1 = ser.submit(t1);
  • 获取结果:boolean r1 = result1.get();
  • 关闭服务:ser.shutdownNow();
package multithreading;  
  
import java.util.concurrent.*;  
  
public class TestCallable implements Callable<Boolean> {  
    private String doWhat;  
 public TestCallable(String doWhat){  
        this.doWhat = doWhat;  
 }  
    @Override  
 public Boolean call() throws Exception {  
        for (int i = 0; i < 10; i++) {  
            System.out.println("我在"+doWhat+"-->"+i);  
 }  
        return true;  
 }  
    public static void main(String[] args) throws ExecutionException, InterruptedException {  
        TestCallable t1 = new TestCallable("学习多线程");  
 TestCallable t2 = new TestCallable("吃饭");  
 TestCallable t3 = new TestCallable("唱歌");  
 //创建执行服务  
 ExecutorService ser = Executors.newFixedThreadPool(3);  
 //提交执行  
 Future<Boolean> result1 = ser.submit(t1);  
 Future<Boolean> result2 = ser.submit(t2);  
 Future<Boolean> result3 = ser.submit(t3);  
 //获取结果  
 boolean r1 = result1.get();  
 boolean r2 = result2.get();  
 boolean r3 = result3.get();  
 //关闭服务  
 ser.shutdownNow();  
 }  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值