zoukankan      html  css  js  c++  java
  • 1、传统线程创建回顾

    1、直接使用Thread新建线程

    package com.yzl;
    
    public class ThreadPart_1 {
        
        //直接使用Thread对象新建线程并重run方法
        public void test1(){
            Thread thread1 = new Thread(){
                @Override
                public void run() {
                    super.run();
                    for(int i=1; i<=10; i++){
                        System.out.println("Thread:" + Thread.currentThread().getName() + ",value is:" + i);
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
            thread1.start();
        }
        
        public static void main(String[] args) {
            System.out.println("Main Thread:" + Thread.currentThread().getName() + " is run ");
            ThreadPart_1 test = new ThreadPart_1();
            test.test1();
        }
    }

    运行结果:

    Main Thread:main is run 
    Thread:Thread-0,value is:1
    Thread:Thread-0,value is:2
    Thread:Thread-0,value is:3
    Thread:Thread-0,value is:4
    Thread:Thread-0,value is:5
    Thread:Thread-0,value is:6
    Thread:Thread-0,value is:7
    Thread:Thread-0,value is:8
    Thread:Thread-0,value is:9
    Thread:Thread-0,value is:10

    2、使用Runnable接口新建线程(从面向对象的角度这方法更好

    //使用Runnable接口新建线程
        public void test2(){
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    for(int i=1; i<=10; i++){
                        System.out.println("Thread:" + Thread.currentThread().getName() + ",value is:" + i);
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            thread.start();
        }
        
        public static void main(String[] args) {
            System.out.println("Main Thread:" + Thread.currentThread().getName() + " is run ");
            ThreadPart_1 test = new ThreadPart_1();
            test.test2();
        }

    运行结果:

    Main Thread:main is run 
    Thread:Thread-0,value is:1
    Thread:Thread-0,value is:2
    Thread:Thread-0,value is:3
    Thread:Thread-0,value is:4
    Thread:Thread-0,value is:5
    Thread:Thread-0,value is:6
    Thread:Thread-0,value is:7
    Thread:Thread-0,value is:8
    Thread:Thread-0,value is:9
    Thread:Thread-0,value is:10

    3、有趣一个测试

    //有趣的测试
        public void test3(){
            Thread thread = new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("Runnable run method is runing~~~");
                    }
                }
            ){
                @Override
                public void run() {
                    System.out.println("Thread run method is runing~~~");
                }
            };
            thread.start();
        }
        
        public static void main(String[] args) {
            System.out.println("Main Thread:" + Thread.currentThread().getName() + " is run ");
            ThreadPart_1 test = new ThreadPart_1();
            test.test3();
        }

    运行结果:

    Main Thread:main is run 
    Thread run method is runing~~~

    原因:

    但是上面这run方法运行的前提是没有人去重写该方法,而我们上面的代码却重写了,导致上面这图片中的代码无法运行,故运行的是thread的run方法。

  • 相关阅读:
    TCP连接异常断开检测(转)
    正排索引与倒排索引(转)
    Elasticsearch之优化
    把网卡中断绑定到CPU,最大化网卡的吞吐量(转)
    十张GIFs让你弄懂递归等概念
    二维数组回形遍历(转)
    如何做Go的性能优化?(转)
    Go的50度灰:Golang新开发者要注意的陷阱和常见错误(转)
    Nginx配置之负载均衡、限流、缓存、黑名单和灰度发布(转)
    从零到卓越:京东客服即时通讯系统的技术架构演进历程(转)
  • 原文地址:https://www.cnblogs.com/yangzhilong/p/4752153.html
Copyright © 2011-2022 走看看