zoukankan      html  css  js  c++  java
  • JAVA高级复习-多线程创建方式一(匿名子类的方式调用线程中的run()方法)

    /**
     * @description:练习:创建两个线程,一个线程遍历50内的偶数,一个线程遍历50内的奇数
     * @date: 2020/7/25 11:02
     * @author: winson
     */
    public class CreateThread2 {
    
        public static void main(String[] args) {
            //1、传统调用方式:实例化后,使用对象调用线程;
            MyThread1 t1 = new MyThread1();
            MyThread2 t2 = new MyThread2();
    //        t1.start();
    //        t2.start();
    
            //2、创建Thread类的匿名子类的方式(面向对象中的知识点)
            new Thread(){
                @Override
                public void run() {
                    for (int i = 0; i < 50; i++) {
                        if (i % 2 == 0) {
                            System.out.println(Thread.currentThread().getName() + ":" + i);
                        }
                    }
                }
            }.start();
    
            //java8中匿名子类的写法
            new Thread(() -> {
                for (int i = 0; i < 50; i++) {
                    if (i % 2 != 0) {
                        System.out.println(Thread.currentThread().getName() + ":" + i);
                    }
                }
            }).start();
        }
    }
    
    
    class MyThread1 extends Thread {
        @Override
        public void run() {
            for (int i = 0; i < 50; i++) {
                if (i % 2 == 0) {
                    System.out.println(Thread.currentThread().getName() + ":" + i);
                }
            }
        }
    }
    
    class MyThread2 extends Thread {
        @Override
        public void run() {
            for (int i = 0; i < 50; i++) {
                if (i % 2 != 0) {
                    System.out.println(Thread.currentThread().getName() + ":" + i);
                }
            }
        }
    }
    
  • 相关阅读:
    python基础--文件操作实现全文或单行替换
    python基础7--集合
    python读写json文件
    python基础6--目录结构
    python基础5--模块
    Ubuntu的一些常用快捷键
    ubuntu dpkg 命令详解
    linux(Ubuntu)安装QQ2013
    fcitx-sogoupinyin下载地址和安装
    Ubuntu下装QQ2014
  • 原文地址:https://www.cnblogs.com/elnimo/p/13375969.html
Copyright © 2011-2022 走看看