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

    一 .概述

    在java之中有两种方式进行线程的创建,

      [1]继承Thread,重写run()方法

      [2]实现Runnable接口,实现run()方法.

      在JUC中的高级接口其实还是这两种方式进行完成的.


    二 .继承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);
            }
        }
    }

    四 . 策略模式

    其实线程的创建方式只有一种,就是创建一个Thread对象.

    那么我们使用Runnable接口的方式又是什么呢?

    这就是一种策略模式的运用.

     

      

  • 相关阅读:
    24. Swap Nodes in Pairs
    23. Merge k Sorted Lists
    shell脚本报错:"[: =: unary operator expected"
    一种用 数组元素 指定 所调函数 的方法
    阻塞 非阻塞
    Linux open() 一个函数,两个函数原型
    QT 执行windows cmd 命令并读取结果
    Qt5 escape spaces in path
    获取磁盘的 总容量,空余容量,已用容量 【windows】
    通过进程名称,获取其路径
  • 原文地址:https://www.cnblogs.com/trekxu/p/8908517.html
Copyright © 2011-2022 走看看