• java并发(一):初探线程的创建


    线程的创建两种方式

    创建线程有四种方式,今天主要演示的是两种:继承Thread,实现Runable接口

    继承Thread创建线程

    import lombok.extern.slf4j.Slf4j;
    
    
    @Slf4j
    class MyThread extends Thread{
        private  int count=0;
        @Override
        public void run(){
            for(int i=0;i<10;i++){
                if(count<10){
                    log.info("{} :counter ={}",this.getName(),count++);
                }
    
            }
    
        }
    
    }
    @Slf4j
    public class ThreadTest {
        public static void main(String[] args) {
           MyThread m1=new MyThread();
           MyThread m2=new MyThread();
           m1.start();
           m2.start();
        }
    }
    

    测试结果

    2019-07-24 14:25:22,725   [Thread-0] INFO  MyThread  - Thread-0 :counter =0
    2019-07-24 14:25:22,725   [Thread-1] INFO  MyThread  - Thread-1 :counter =0
    2019-07-24 14:25:22,725   [Thread-1] INFO  MyThread  - Thread-1 :counter =1
    2019-07-24 14:25:22,725   [Thread-1] INFO  MyThread  - Thread-1 :counter =2
    2019-07-24 14:25:22,725   [Thread-0] INFO  MyThread  - Thread-0 :counter =1
    2019-07-24 14:25:22,725   [Thread-0] INFO  MyThread  - Thread-0 :counter =2
    2019-07-24 14:25:22,725   [Thread-1] INFO  MyThread  - Thread-1 :counter =3
    2019-07-24 14:25:22,725   [Thread-0] INFO  MyThread  - Thread-0 :counter =3
    2019-07-24 14:25:22,725   [Thread-1] INFO  MyThread  - Thread-1 :counter =4
    2019-07-24 14:25:22,725   [Thread-1] INFO  MyThread  - Thread-1 :counter =5
    2019-07-24 14:25:22,725   [Thread-1] INFO  MyThread  - Thread-1 :counter =6
    2019-07-24 14:25:22,725   [Thread-1] INFO  MyThread  - Thread-1 :counter =7
    2019-07-24 14:25:22,725   [Thread-1] INFO  MyThread  - Thread-1 :counter =8
    2019-07-24 14:25:22,725   [Thread-1] INFO  MyThread  - Thread-1 :counter =9
    2019-07-24 14:25:22,725   [Thread-0] INFO  MyThread  - Thread-0 :counter =4
    2019-07-24 14:25:22,741   [Thread-0] INFO  MyThread  - Thread-0 :counter =5
    2019-07-24 14:25:22,741   [Thread-0] INFO  MyThread  - Thread-0 :counter =6
    2019-07-24 14:25:22,741   [Thread-0] INFO  MyThread  - Thread-0 :counter =7
    2019-07-24 14:25:22,741   [Thread-0] INFO  MyThread  - Thread-0 :counter =8
    2019-07-24 14:25:22,741   [Thread-0] INFO  MyThread  - Thread-0 :counter =9
    
    Process finished with exit code 0
    
    

    说明

    两个线程,各自的变量,交替运行

    实现Runable接口创建线程

    import lombok.extern.slf4j.Slf4j;
    
    @Slf4j
    public class RunableTest {
        public static void main(String[] args) {
            //内部类寄生于外部类,需要先创建外部类的对象
            RunableTest test = new RunableTest();
            MyThread m1 = test.new MyThread();
            //两个线程共享一个对象
            Thread t1 = new Thread(m1);
            Thread t2 = new Thread(m1);
            t1.start();
            t2.start();
    
        }
    
        class MyThread implements Runnable {
            public int count = 0;
    
    
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    //runable只继承了getClass方法
                    if(count<10){
                        log.info("{} :counter ={}", Thread.currentThread().getName(), count++);
                    }
    
                }
            }
        }
    
    }
    

    测试结果

    
    2019-07-24 14:26:40,722   [Thread-1] INFO  RunableTest  - Thread-1 :counter =1
    2019-07-24 14:26:40,722   [Thread-0] INFO  RunableTest  - Thread-0 :counter =0
    2019-07-24 14:26:40,737   [Thread-0] INFO  RunableTest  - Thread-0 :counter =3
    2019-07-24 14:26:40,737   [Thread-0] INFO  RunableTest  - Thread-0 :counter =4
    2019-07-24 14:26:40,737   [Thread-0] INFO  RunableTest  - Thread-0 :counter =5
    2019-07-24 14:26:40,737   [Thread-0] INFO  RunableTest  - Thread-0 :counter =6
    2019-07-24 14:26:40,737   [Thread-0] INFO  RunableTest  - Thread-0 :counter =7
    2019-07-24 14:26:40,737   [Thread-0] INFO  RunableTest  - Thread-0 :counter =8
    2019-07-24 14:26:40,737   [Thread-0] INFO  RunableTest  - Thread-0 :counter =9
    2019-07-24 14:26:40,737   [Thread-1] INFO  RunableTest  - Thread-1 :counter =2
    
    Process finished with exit code 0
    

    说明

    两个线程,共享一个对象,交替运行。

  • 相关阅读:
    pku3486Computers 动态规划
    pku2229sumsets(zjgsu,分花)
    pku2663Tri Tiling递推题
    pku1015Jury Compromise 动态规划
    pku3508Hide That Number一道加密算法题
    pku动态规划题目列表
    浅谈XXE攻击
    PHP核心配置详解
    SSRF漏洞用到的其他协议(dict协议,file协议)
    php中使用CURL之php curl详解
  • 原文地址:https://www.cnblogs.com/JuncaiF/p/11246823.html
Copyright © 2020-2023  润新知