Java多线程-概述

Java多线程-概述

概述

 java.thread 多线程
 线程实现
 线程状态
 线程同步
 线程通信
 高级主题

简介

 多任务
    同时在做多个任务 像是调用方法 进去再出来
 多线程
    多道路解决单道路堵塞的问题 两条线并行
 进程&线程 process&thread
    单个进程里面有多个线程 视频 音频 弹幕
    理论上应该多核的cpu才可以执行多个线程
    单核的话其实是切换的特别快
 程序
    程序运行中其实肯定有多个线程,例如main线程,gc线程(垃圾回收)
 线程
    有自己的内存空间

Thread类

 自定义线程继承Thread类 
    实现借口Runnable借口 implemnets Runnable
 创建线程的方法
    自定义线程一个类继承Thread类 
    重写run()方法,编写线程执行体
    创建线程对象,调用start()方法启动线程
 run()和start()
    调用run()相当于主线程调用函数,start()才是调用方法
 总结
    注意:线程开启不一定立即执行,由cpu调度,开启线程要用start()

案例1

package com.alibaba;

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

//继承Thread类,重写类方法run(),调用start()方法启动线程
public class TestThread extends Thread {
    private String url;
    private String fileName;

    public  TestThread(){
    }
    public TestThread(String url,String fileName){
        this.url = url;
        this.fileName = fileName;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    @Override
    public void run() {
        WebDownloader webDownloader  = new WebDownloader();
        webDownloader.downLoader(url,fileName);
        System.out.println("下载的文件名为:"+fileName);

    }

    public static void main(String[] args) {
        TestThread testThread1 = new TestThread("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F650136a8-b170-43a0-ad8f-54b51c8f311b%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1693824710&t=96474a7ac79672629bdffe0e2307c6af","test1.img");
        TestThread testThread2 = new TestThread("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F79ba7324-6836-4706-90e0-d04d4674062f%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1693824710&t=9044005fa39cf28fb39de6ab948a37ab","test2.img");
        TestThread testThread3 = new TestThread("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F59b2656c-b80a-4073-907d-b2897a910733%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1693824710&t=a2e1cd352851cd805cc1d11b788923c7","test3.img");

        testThread1.start();
        testThread2.start();
        testThread3.start();
    }
}

class WebDownloader {
//    public WebDownloader(){
//
//    }
    //下载方法
    public void downLoader(String url,String fileName){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(fileName));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常,downloder方法异常!");
        }
    }
}

Runnable接口

 可以构造类继承Runnable接口
    也需要实现run()方法
 推荐使用Runnable接口的方法来实现
    好处: 避免单继承局限性,灵活使用,方便同一个对象被多个线程使用
package com.alibaba;

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

    public static void main(String[] args) {
        TestThread001 testThread001 = new TestThread001();
//        Thread thread001 = new Thread(testThread001);
//        thread001.start();
        new Thread(testThread001).start();
        for (int i = 0; i < 200; i++) {
            System.out.println("测试Runnable接口");
        }
    }
}

并发

 多个线程操控同一个资源的时候,线程不安全,数据会紊乱
package com.alibaba;

public class TestThread002 implements Runnable{

    private int ticketNumbs = 20;

    @Override
    public void run() {
        while(true){
            if (ticketNumbs <= 0){
                break;
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"拿到了第"+ ticketNumbs--+"张票");
        }
    }

    public static void main(String[] args) {
        TestThread002 testThread002 = new TestThread002();
        new Thread(testThread002,"小明").start();
        new Thread(testThread002,"小黄").start();
        new Thread(testThread002,"黄牛").start();
    }
}
posted @   淡漠灬白驹  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示