• 实现多线程最简单的两种方式


    多线程(推荐使用方式2)

    创建多线程方式1:继承Thread类,重写run方法,调用start()方法

    /**
     * @author 颂梓枫
     * @since JDK 1.8
     * ClassName: ThreadDemo
     * date: 2021/1/1 22:19
     * Description:
     * 创建多线程方式1:1。继承Thread类
     *                2。重写run方法
     *                3。调用start()方法
     */
    public class ThreadDemo extends Thread {
        //重写Thread方法
        @Override
        public void run() {
            for (int i = 0; i < 30; i++) {
                System.out.println("run方法"+i);
            }
        }
        public static void main(String[] args) {
            ThreadDemo threadDemo = new ThreadDemo();
            //调用start()方法
            threadDemo.start();
    
            for (int i = 0; i < 30; i++) {
                System.out.println("main方法--->"+i);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    运行结果(部分)

    main方法--->16
    main方法--->17
    main方法--->18
    run方法0
    run方法1
    run方法2
    main方法--->19
    main方法--->20
    main方法--->21
    main方法--->22
    main方法--->23
    main方法--->24
    main方法--->25
    main方法--->26
    main方法--->27
    main方法--->28
    main方法--->29
    run方法3
    run方法4
    run方法5
    run方法6
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    创建多线程方式2:实现runable接口,重写run方法,调用

    注意:线程开启不一定立即执行,由CPU调度

    创建多线程方式2:.实现runnable接口 ,.重写run方法 , .创建线程对象调用start()方法

    /**
     * @author 颂梓枫
     * @since JDK 1.8
     * ClassName: ThreadRun
     * date: 2021/1/1 23:40
     * Description:
     * 创建多线程方式2:1.实现runnable接口
     *                2.重写run方法
     *                3.创建线程对象,调用start()方法
     */
    public class ThreadRun implements Runnable {
        @Override
        public void run() {
            for (int i = 0; i < 20; i++) {
                System.out.println("run--->"+i);
            }
        }
        public static void main(String[] args) {
            //创建Runnable接口的实现类对象
            ThreadRun threadRun = new ThreadRun();
            //创建线程对象,通过线程对象开启线程
            Thread thread = new Thread(threadRun);
            thread.start();
            //或直接执行  new Thread(threadRun).start();
            for (int i = 0; i < 20; i++) {
                System.out.println("main--->"+i);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    运行结果

    run--->4
    run--->5
    main--->1
    run--->6
    run--->7
    run--->8
    main--->2
    main--->3
    main--->4
    main--->5
    run--->9
    run--->10
    run--->11
    原文章:
  • 相关阅读:
    python-进程池实例
    python-进程通过队列模拟数据的下载
    python-多进程模板
    python-多线程同步中创建互斥锁解决资源竞争的问题
    CentOS6.5配置网络
    解决CentOS系统Yum出现"Cannot find a valid baseurl for repo"问题
    CentOS 6.5安装图形界面
    Centos安装git
    Web前端优化,提高加载速度
    谁说写代码的不懂生活
  • 原文地址:https://www.cnblogs.com/tfil/p/14228323.html
Copyright © 2020-2023  润新知