Java 多线程------多线程的创建(2),方式一:继承于Thread类
1 package com.bytezero.threadexer; 2 3 /** 4 * 创建两个分线程,其中一个线程遍历100以内的偶数,另一个线程遍历100以内的奇数 5 * 6 * 7 * @author Bytezero1·zhenglei! Email:420498246@qq.com 8 * create 2021-10-15 10:45 9 */ 10 11 12 13 public class ThreadDemo { 14 public static void main(String[] args) { 15 // MyThreadEven e1 = new MyThreadEven(); 16 // MyThreadOdd o1 = new MyThreadOdd(); 17 // 18 // e1.start(); 19 // o1.start(); 20 21 //创建 Thread类的拿匿名子类的方式 22 new Thread(){ 23 @Override 24 public void run() { 25 for (int i = 0; i < 100; i++) { 26 if( i % 2 ==0){ 27 System.out.println(Thread.currentThread().getName()+":"+i); 28 } 29 } 30 } 31 }.start(); 32 33 new Thread(){ 34 @Override 35 public void run() { 36 for (int i = 0; i < 100; i++) { 37 38 if(i %2 != 0){ 39 System.out.println(Thread.currentThread().getName()+":"+i); 40 } 41 } 42 } 43 }.start(); 44 45 } 46 } 47 48 class MyThreadEven extends Thread{ 49 @Override 50 public void run() { 51 for (int i = 0; i < 100; i++) { 52 if( i % 2 ==0){ 53 System.out.println(Thread.currentThread().getName()+":"+i); 54 } 55 } 56 } 57 } 58 59 class MyThreadOdd extends Thread{ 60 61 @Override 62 public void run() { 63 for (int i = 0; i < 100; i++) { 64 65 if(i %2 != 0){ 66 System.out.println(Thread.currentThread().getName()+":"+i); 67 } 68 } 69 } 70 }
....................
本文来自博客园,作者:Bytezero!,转载请注明原文链接:https://www.cnblogs.com/Bytezero/p/15412320.html