zoukankan      html  css  js  c++  java
  • 实现多线程最简单的两种方式

    多线程(推荐使用方式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
    原文章:
  • 相关阅读:
    博客园博客
    mongo
    函数式编程与面向对象编程的对比
    python_字典dict要点总结
    pyhon_列表、元组要点总结
    vue-element框架通过blob进行后端token权限验证下载
    node-本地搭建服务
    (转载)测试用例标准
    soapUI学习笔记--用例字段参数化
    soapUI学习笔记---断言的小使用
  • 原文地址:https://www.cnblogs.com/tfil/p/14228323.html
Copyright © 2011-2022 走看看