首页 > 编程语言 >Java入门12(多线程)

Java入门12(多线程)

时间:2023-07-12 12:11:32浏览次数:38  
标签:12 Java Thread void System 线程 new 多线程 public

多线程

线程的实现方式

  1. 继承 Thread 类:一旦继承了 Thread 类,就不能再继承其他类了,可拓展性差
  2. 实现 Runnable 接口:仍然可以继承其他类,可拓展性较好
  3. 使用线程池

继承Thread 类

​ 不能通过线程对象调用 run() 方法,需要通过 t1.start() 方法,使线程进入到就绪状态,只要进入到就绪状态的线程才有机会被JVM调度选中

// 这是一个简单的栗子
public class StudentThread extends Thread{
    public StudentThread(String name) {
        super(name);
    }
    @Override
    public void run() {
        for (int i = 0; i < 2; i++) {
            System.out.println("This is a test thread!");
            System.out.println(this.getName());
        }
    }
}
// 启动类
public static void main(String[] args) {
    Thread t1 = new StudentThread();
    // 不能通过线程对象调用run()方法
    // 通过 t1.start() 方法,使线程进入到就绪状态,只要进入到就绪状态的线程才有机会被JVM调度选中
    t1.start();
}

实现 Runable 接口

​ 实现方式需要借助 Thread 类的构造函数,才能完成线程对象的实例化

// 介还是一个简单的栗子
public class StudentThreadRunnable implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 2; i++) {
            System.out.println("This is a test thread!");
            System.out.println(Thread.currentThread().getName());
        }
    }
}
// 启动类
public static void main(String[] args) {
    // 实现方式需要借助 Thread 类的构造函数,才能完成线程对象的实例化
    StudentThreadRunnable studentThreadRunnable = new StudentThreadRunnable();
    Thread t01 = new Thread(studentThreadRunnable);
    t01.setName("robot010");
    t01.start();
}

匿名内部类实现

​ 在类中直接书写一个当前类的子类,这个类默认不需要提供名称,类名由JVM临时分配

public static void main(String[] args) {
    Thread t01 = new Thread(){
        @Override
        public void run() {
            for (int i = 0; i < 2; i++) {
                System.out.println("This is a test thread!");
            }
            System.out.println(Thread.currentThread().getName()); // 线程名
            System.out.println(this.getClass().getName()); // 匿名线程类类名
        }
    };
    t01.start();
}

线程的休眠(sleep方法)

​ sleep方法,会使当前线程暂停运行指定时间,单位为毫秒(ms),其他线程可以在sleep时间内,获取JVM的调度资源

// 这是一个计时器
public class TimeCount implements Runnable{
    @Override
    public void run() {
        int count = 0;
        while(true){
            System.out.println(count);
            count++;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
// 测试类
public static void main(String[] args) {
    System.out.println("这是main方法运行的时候,开启的主线程~~~");
    TimeCount timeCount = new TimeCount();
    Thread timeThread = new Thread(timeCount);
    System.out.println("开启计时器");
    timeThread.start();

    System.out.println("主线程即将休眠>>>>>>>>>>>");
    try {
        Thread.sleep(20000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println(">>>>>>>>>>>主线程休眠结束~~~~~");
}

线程的加入(join方法)

​ 被 join 的线程会等待 join 的线程运行结束之后,才能继续运行自己的代码

public static void main(String[] args) {
    Thread thread01 = new Thread(){
        @Override
        public void run(){
            for (int i = 0; i < 10; i++) {
                System.out.println("This is thread-01!");
            }
        }
    };
    thread01.start();
    try {
        thread01.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    Thread thread02 = new Thread(){
        @Override
        public void run(){
            for (int i = 0; i < 10; i++) {
                System.out.println("This is thread-02!");
            }
        }
    };
    thread02.start();
}
// thread02 会等待 thread01 完全跑完,才会开始自己的线程

线程的优先级(priority方法)

​ 优先级高的线程会有更大的几率竞争到JVM的调度资源,但是高优先级并不代表绝对,充满玄学✨

public static void main(String[] args) {
    Thread thread01 = new Thread(){
        @Override
        public void run(){
            for (int i = 0; i < 10; i++) {
                System.out.println("This is thread-01! " + Thread.currentThread().getPriority());
            }
        }
    };
    Thread thread02 = new Thread(){
        @Override
        public void run(){
            for (int i = 0; i < 10; i++) {
                System.out.println("This is thread-02! " + Thread.currentThread().getPriority());
            }
        }
    };
    thread01.setPriority(1);
    thread02.setPriority(10);
    // 尽管thread02优先级高于thread01,但是也有可能
    thread01.start();
    thread02.start();
}

线程的让步(yield方法)

​ 立刻让出JVM的调度资源,并且重新参与到竞争中

public static void main(String[] args) {
    Thread thread01 = new Thread(){
        @Override
        public void run(){
            for (int i = 1; i <= 10; i++) {
                System.out.println("This is thread-01! " + i);
            }
        }
    };
    Thread thread02 = new Thread(){
        @Override
        public void run(){
            for (int i = 1; i <= 10; i++) {
                System.out.println("This is thread-02! " + i);
                Thread.yield();
            }
        }
    };
    thread01.start();
    thread02.start();
}

守护线程(Deamon)

​ 会在其他非守护线程都运行结束之后,自身停止运行,(GC垃圾回收机制就是一个典型的守护线程)

public static void main(String[] args) {
    Thread thread01 = new Thread(){
        @Override
        public void run(){
            int times = 0;
            while(true){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("time pass " + ++times + "second");
            }
        }
    };
    Thread thread02 = new Thread(){
        @Override
        public void run(){
            for (int i = 1; i <= 10; i++) {
                System.out.println("This is thread-02! " + i);
            }
        }
    };
    // 将t1设置为守护线程
    thread01.setDaemon(true);
    thread01.start();
    thread02.start();
    // 延长主线程运行,便于观察结果
    try {
        Thread.sleep(20000);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    System.out.println("main thread end \\(-_-)/");
}

线程同步

数据操作的原子性

​ 具有原子性的操作,不会被其他线程打断,类似(a++)的操作是不具备原子性的,因此很容易在多线程场景中出现误差

synchronized 悲观锁(互斥性)

优缺点:保证了数据在多线程场景下的安全(保证线程安全),牺牲的是效率,锁的获取和释放,其他线程被阻塞都会额外消耗性能

同步对象:被多个线程所竞争的资源对象叫做同步对象

核心作用: 确保线程在持有锁的期间内,其他线程无法操作和修改指定数据(同步对象)

​ 每一个同步对象都会持有一把线程锁,当线程运行到synchronized 修饰的方法或代码时,线程会自动获取当前同步对象的线程锁,在synchronized 修饰的方法或代码块运行结束后,该线程会自动释放此线程锁,在持有线程锁的这段时间里,其他线程是无法执行synchronized 所修饰的代码块的,其他线程会被阻塞在synchronized 代码块之外,直到这把锁被释放。。。

// synchronized 的两种写法:
// 1. 写在方法之前,修饰整个方法
public synchronized Ticket getTicket(){
    // 取票
    Ticket ticketTmp = null;
    if(!tickets.isEmpty()){
        ticketTmp = tickets.removeLast();
    }
    return ticketTmp;
}
// 2. 代码块,修饰代码块所包含的部分
public Ticket getTicket(){
    // 取票
    synchronized(this){
        Ticket ticketTmp = null;
        if(!tickets.isEmpty()){
            ticketTmp = tickets.removeLast();
        }
        return ticketTmp;
    }
}

线程死锁

标签:12,Java,Thread,void,System,线程,new,多线程,public
From: https://www.cnblogs.com/te9uila/p/17547178.html

相关文章

  • 基于JavaFX的扫雷游戏实现(五)——设置和自定义控件
      它来了它来了,最后一期终于来了。理论上该讲的全都讲完了,只剩下那个拖了好几期的自定义控件和一个比较没有存在感的设置功能没有讲。所以这次就重点介绍它们俩吧。  首先我们快速浏览下设置的实现,上图:  然后是控制器代码:SettingsController.javapackagecontrollers;......
  • Json与Java对象互转工具类
    常用的JSON处理库:Jackson:这是最常用的库,它提供了各种特性,如生成/解析JSON的速度快,内存占用小,可扩展性强等。Jackson支持类型安全,还具有复杂数据绑定的能力。Gson:由Google开发,也是一个相当流行的库,使用起来非常简单。Gson可以工作在任何JDK版本上,没有任何额外的依赖项......
  • [TM4] TM4C123G Keil5 新建工程指南
    [TM4]TM4C123GKeil5新建工程指南keil新建工程,选择TM4C123GH6PM芯片,然后在CMSIS勾选CORE,DEVICE勾选Startup(如图),来到新工程界面在SourceGroup1里添加main.c,将SourceGroup重命名为user在Target1目录下添加driverlib文件夹、hardware文件夹、sys文件夹、inc文件夹,utils文件夹以......
  • 如何实现java环境安装的具体操作步骤
    Java环境安装Java是一种跨平台的编程语言,可以在不同的操作系统上运行。在开始学习和使用Java之前,我们需要先安装Java环境。本文将介绍如何在Windows、Mac和Linux操作系统上安装Java,并提供相关代码示例。Windows操作系统在Windows操作系统上安装Java需要进行以下步骤:下载Java......
  • LeetCode 热题 100 之 128. 最长连续序列
    题目描述给定一个未排序的整数数组nums,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。请你设计并实现时间复杂度为 O(n)的算法解决此问题。示例1:输入:nums=[100,4,200,1,3,2]输出:4解释:最长数字连续序列是[1,2,3,4]。它的长度为4。示例2:输入:nums......
  • 20230712 讲题
    CF1364DEhab'sLastCorollary简单题。特判掉\(m=n-1\)的情况,此时一定能找到一个大小为\(\left\lceil\frac{k}{2}\right\rceil\)的独立集,二分图染色即可。否则,我们建出dfstree,找到一条连接的两个端点深度之差最小的返祖边,设它为\((u,v)\),且\(dep_u>dep_v\)。......
  • Unparseable date: "2023-07-12T01:43:06.000Z"
    1、调用硬件的接口,返回的数据,存到数据库,时间格式是UTC的不是北京的,要转一下,北京=UTC+8小时2、具体代码publicstaticStringforBeiJing(Stringtime){try{SimpleDateFormatsdf1=newSimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");Datedate1=sdf......
  • C++计算机学院2023年度小学期编程实践课程(图书管理系统)[2023-07-12]
    C++计算机学院2023年度小学期编程实践课程(图书管理系统)[2023-07-12]计算机学院2023年度小学期编程实践课程上机实验题目(一)基于学生结构体数组的图书管理系统(40分)定义学生结构体类型的数组,静态初始化学生信息(不包括借书信息)。图书使用二维字符数组或字符指针数组单独......
  • 2023-07-12 vue this.$set设置子组件内的值无效(uniapp+vue)
    前言:怎么说呢,子组件内嵌套了多层对象和数组,业务逻辑也是在子组件内处理,如何修改多层嵌套的对象数组的值?vue提供了一个this.$set方法去改变对应的值,实测在uniapp打包的微信小程序中无法使用该方法,而在Android端则可以,那有没有两全其美的方法?答案是有,在修改深层次的值时可以通过先......
  • 使用Java8 Stream流中的Collectors.collectingAndThen()方法去重
    https://blog.csdn.net/qq_40474184/article/details/122043378Stream流的常用方法大全https://blog.csdn.net/weixin_52317961/article/details/128117727......