package leetcode;
public class ThreadTest extends Thread{
@Override
public void run(){
for (int i = 0; i < 100; i++) {
if (i % 2 == 0){
System.out.println(Thread.currentThread().getName()+":\t"+i);
}
}
}
public static void main(String[] args) {
ThreadTest threadTest = new ThreadTest();
threadTest.start();
ThreadTest threadTest1 = new ThreadTest();
threadTest1.start();
}
}
第二种 实现Runnable 接口
package leetcode.thread_test;
public class RunTest {
public static void main(String[] args) {
Thread thread = new Thread(new MyRunnable());
Thread thread1 = new Thread(new MyRunnable());
thread.start();
thread1.start();
}
}
class MyRunnable implements Runnable{
@Override
public void run(){
for (int i = 0; i < 100; i++) {
if (i%2 == 0){
System.out.println(Thread.currentThread().getName()+":\t"+i);
}
}
}
}