线程练习题
作者:互联网
线程练习题
package sq.com.exer;
/**
* 练习:创建两个分线程,其中一个线程遍历 100 以内的偶数,另一个线程遍历 100 以内的奇数
*/
public class ThreadDemo {
public static void main(String[] args) {
// MyThread1 m1 = new MyThread1();
// MyThread1 m2 = new MyThread1();
// m1.start();
// m2.start();
// 创建 Thread 类的匿名子类的方式
new Thread(){
public void run() {
for (int i = 0; i < 100; i++) {
if(i % 2 == 0){
System.out.println(Thread.currentThread().getName() + " : " + i);
}
}
}
}.start();
new Thread(){
public void run() {
for (int i = 0; i < 100; 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 < 100; 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 < 100; i++) {
if(i % 2 != 0){
System.out.println(Thread.currentThread().getName() + " : " + i);
}
}
}
}
*/
标签:练习题,Thread,void,线程,MyThread1,100,public 来源: https://blog.csdn.net/sunqianLOL/article/details/118070282