/**
* @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);
}
}
}
}