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接口的方式又是什么呢?

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

     

      

  • 相关阅读:
    SPOJ LCS2
    SPOJ NSUBSTR
    1977: [BeiJing2010组队]次小生成树 Tree
    2002: [Hnoi2010]Bounce 弹飞绵羊
    P3690 【模板】Link Cut Tree (动态树)
    P2093 [国家集训队]JZPFAR
    2648: SJY摆棋子
    HDU 2966 In case of failure
    bzoj 一些题目汇总
    BZOJ3653谈笑风生——可持久化线段树+dfs序
  • 原文地址:https://www.cnblogs.com/trekxu/p/8908517.html
Copyright © 2011-2022 走看看