public class Test { public static void main(String[] args) { MyThread mt = new MyThread(); // 实例化线程对象 mt.start();// 启动线程 } } // 通过继承Thread类来创建线程 class MyThread extends Thread { private int count = 0; public void run() { while (count < 100) { count++; if (count % 2 == 0) System.out.println("count=" + count); } } }
public class Test { public static void main(String[] args) { Thread thread = new Thread(new MyThread()); //创建线程实例 thread.start(); //启动线程 } } /**通过实现Runnable接口来创建线程类*/ class MyThread implements Runnable{ private int count=0; @Override public void run() { while(count<100){ count++; if(count%2==0) System.out.println("count="+count); } } }