多线程基础

多线程

藏好自己,做好清理

实际上是多个CPU,即多核,如果一个CPU情况下,其实是一个时刻处理一个线程,一段时间内处理多个线程

1. 进程与线程

进程是程序的一次动态执行过程,它经历了从代码加载(产生)、执行(发展)到执行完毕(消亡)的一个完整过程。每个进程都能循环获得自己的CPU时间片,CPU执行速度非常快,所以好像同时运行一样。

1.1 打开WPS是打开一个程序,也相当于启动了一个进程,WPS的拼音检查就是一个线程,可以有很多的线程在进程上,PID的P就是Process【进程】

1.2 在一个进程中,自己没有创建线程,后台也会有多个线程,如main()【主线程】,gc线程【垃圾回收线程】

1.3 单线程:run()方法先执行完再返回到主方法

1.4 多线程:start()方法和主方法一起并发执行(时间段);在时刻上还是执行其中一个

2. 多线程实现

2.1 继承Thread类(java.lang.Thread)

  • 栗子1:
//创建线程方式一:继承Thread类,重写run方法,调用start开启线程
package com.xxy.thread;
//创建线程方式一:继承Thread类,重写run方法,调用start开启线程
public class TestThread extends Thread{
   
    @Override
    public void run() {
   
        //run方法线程体
        for (int i = 0; i < 10; i++) {
   
            System.out.println("我在看代码" + i);
        }
    }

    public static void main(String[] args) {
   
        //main方法线程,主线程
       TestThread testThread = new TestThread();
        //调用start()开启线程
        testThread.start();
       //testThread.run();
        for (int i = 0; i < 10; i++) {
   
            System.out.println("我在学习多线程" + i);
        }
    }
}
  • 栗子2:下载多张图片

下载Apache Commons IO

package com.xxy.thread;

import org.apache.commons.io.FileUtils;

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

//实现多线程下载图片
public class TestThreadDownload extends Thread{
   
    private String url;
    private String name;

    public TestThreadDownload(String url,String name) {
   
        this.url = url;
        this.name = name;
    }
//下载图片执行体
    @Override
    public void run() {
   
        WebDownloader webDownloader = new WebDownloader();
        webDownloader.downloader(url,name);
        System.out.println("下载了文件名为:" + name);
    }

    public static void main(String[] args) {
   
        TestThreadDownload t1 = new TestThreadDownload("https://img-home.csdnimg.cn/images/20210624094420.png","1.png");
        TestThreadDownload t2 = new TestThreadDownload("https://img-bss.csdn.net/1623287748640.png","2.png");
        TestThreadDownload t3 = new TestThreadDownload("https://img-home.csdnimg.cn/images/20210426034059.png","3.png");
        t1.start();
        t2.start();
        t3.start();
    }
}

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方法出现问题");
        }
    }
}

  • 为什么线程启动必须调用start()方法执行而不是直接调用run()方法

start()方法源码:

public synchronized void start() {
   
        if (this.threadStatus != 0) {
   
            throw new IllegalThreadStateException();//运行时异常
        } else {
   
            this.group.add(this);
            boolean var1 = false;

            try {
   
                this.start0();//方法上使用了native关键字(java本地接口调用)调用本机操作系统的函数功能完成特殊操作
                var1 = true;
            } finally {
   
                try {
   
                    if (!var1) {
   
                        this.group.threadStartFailed(this);
                    }
                } catch (Throwable var8) {
   
                }

            }

        }
    }
 private native void start0();

为了java的可移植性,只在意调用操作系统的函数功能来实现start0()方法

  • 缺点

单继承局限

2.2 实现Runnable接口

避免了单继承的局限性

  • Runnable接口源码
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package java.lang;

@FunctionalInterface //Jdk1.8引入Lambda表达式变为函数式接口
public interface Runnable {
   
    void run();
}

  • 栗子1:
package com.xxy.thread;
//创建线程方法2:实现Runnable接口,重写run方法,执行线程时需要丢入Runnable接口实现类,调用strat方法
public class TestThread2 implements Runnable {
   
    @Override
    public void run() {
   
        //run方法线程体
        for (int i = 0; i < 10; i++) {
   
            System.out.println("我在看代码" + i);
        }
    }

    public static void main(String[] args) {
   
        //main方法线程,主线程

        TestThread2 testThread2 = new TestThread2();
//创建一个线程对象,通过线程对象来开启我们的线程,代理
        //Thread thread = new Thread(testThread2);
        //thread.start();

        //调用start()开启线程
        new Thread(testThread2).start();
        for (int i = 0; i < 10; i++) {
   
            System.out.println("我在学习多线程" + i);
        }
    }
}

  • 栗子2:lambda表达式,jdk1.8新特性
package com.xxy.thread;
//创建线程方法2:实现Runnable接口,重写run方法,执行线程时需要丢入Runnable接口实现类,调用strat方法
public class TestThread3 implements Runnable {
   
    @Override
    public void run() {
   

    }

    public static void main(String[] args) {
   
        //main方法线程,主线程

        //调用start()开启线程 ,lambda表达式
        new Thread(() -> {
   
            for (int i = 0; i < 10; i++) {
   
                System.out.println("我在看代码" + i);
            }
        }).start();

        for (int i = 0; i < 10; i++) {
   
            System.out.println("我在学习多线程" + i);
        }
    }


}

  • 栗子3:多线程下载图片
package com.xxy.test;

import com.xxy.thread.TestThreadDownload;
import org.apache.commons.io.FileUtils;

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

//实现多线程下载图片
public class TestRunableDownload implements Runnable{
   
    private String url;
    private String name;

    public TestRunableDownload(String url,String name) {
   
        this.url = url;
        this.name = name;
    }
    //下载图片执行体
    @Override
    public void run() {
   
        WebDownloader webDownloader = new WebDownloader();
        webDownloader.downloader(url,name);
        System.out.println("下载了文件名为:" + name);
    }

    public static void main(String[] args) {
   
        TestThreadDownload t1 = new TestThreadDownload("https://img-home.csdnimg.cn/images/20210624094420.png","1.png");
        TestThreadDownload t2 = new TestThreadDownload("https://img-bss.csdn.net/1623287748640.png","2.png");
        TestThreadDownload t3 = new TestThreadDownload("https://img-home.csdnimg.cn/images/20210426034059.png","3.png");

        new Thread(t1).start();
        new Thread(t2).start();
        new Thread(t3).start();
    }
}

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方法出现问题");
        }
    }
}

  • 栗子4:并发问题
package com.xxy.thread;


//卖车票
//发现问题:并发,数据乱
public class TestThread4 implements Runnable{
   
    private int tickNums = 10;
    @Override
    public void run() {
   
        while (true) {
   
            if (tickNums <= 0) {
   
                break;
            }

            try {
   
                Thread.sleep(200);
            } catch (InterruptedException e) {
   
                e.printStackTrace();
            }

            System.out.println(Thread.currentThread()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值