zoukankan      html  css  js  c++  java
  • 001 线程的创建和启动

    一 . 概述

    在jdk之中包含两种线程的创建方式,并统一使用start()方法进行线程的启动.


    二 .继承Thread 来创建线程

    public class CreateThread {
        public static void main(String[] args) {
            //创建线程一并完成任务一
                    new Thread() {
                        @Override
                        public void run() {
                            task1();
                        }
                        
                    }.start();
                    
                    new Thread() {
                        @Override
                        public void run() {
                            task2();
                        };
                    }.start();
        }
    
        public static void task1() {
            for(int i=0;i<20;i++) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("task1===>"+i);
            }
        }
        public static void  task2() {
            for(int i=0;i<20;i++) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("task2===>"+i);
            }
        }
    }

    三 ,实现Runnable 接口创建线程

    public class UseRunnable {
        public static void main(String[] args) {
            new Thread(new Runnable() {
                public void run() {
                    task1();
                }
            }).start();
            
            new Thread(new Runnable() {
                public void run() {
                    task2();
                }
            }).start();
        }
        public static void task1() {
            for(int i=0;i<20;i++) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("task1===>"+i);
            }
        }
        public static void  task2() {
            for(int i=0;i<20;i++) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("task2===>"+i);
            }
        }
    }

    四 .两种方法的比较

    [1] 两种方式其实是一种方式,只不过使用Runnable接口来抽离线程逻辑单元.

    [2] 我们一般情况下更喜欢使用Runnable的方式来创建线程.

    [3] Runnable的方式类似于策略设计模式.

    总结 : 在下面我们说一下策略设计模式.

  • 相关阅读:
    [单调栈] Jzoj P4260 最大子矩阵
    [前缀和] Jzoj P4259 矩形
    [欧拉回路][状压dp] Jzoj P3290 吃货JYY
    [组合数][枚举] Jzoj P3332 棋盘游戏
    [欧拉函数][dp][快速幂] Jzoj P1161 机器人M号
    [exgcd] Jzoj P1158 荒岛野人
    [带权并查集] Jzoj P1503 体育场
    [dfs][树的直径] Jzoj P1737 删边
    [差分][倍增lca][tarjan] Jzoj P3325 压力
    [dfs] Jzoj P1497 景点中心
  • 原文地址:https://www.cnblogs.com/trekxu/p/8969250.html
Copyright © 2011-2022 走看看