zoukankan      html  css  js  c++  java
  • 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
    

    说明

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

  • 相关阅读:
    为什么说 LINQ 要胜过 SQL
    统一日志系统 Log4Net/ExceptionLess
    使用Advanced Installer制作IIS安装包(二:配置安装包依赖项和自定义dll)
    使用Advanced Installer制作IIS安装包(一:配置IIS和Web.config)
    安全的API接口解决方案
    任务调度及远端管理(基于Quartz.net)
    关于.NET参数传递方式的思考
    C# Parsing 类实现的 PDF 文件分析器
    .NET的弹性及瞬间错误处理库Polly
    Mybatis官方网站
  • 原文地址:https://www.cnblogs.com/JuncaiF/p/11246823.html
Copyright © 2011-2022 走看看