publicclassThreadTest{publicstaticvoidmain(String[] args){
SubTest subTest =newSubTest();
subTest.start();}}classSubTestextendsThread{@Overridepublicvoidrun(){for(int i =2; i <100; i++){if(i %2==0){
System.out.println(Thread.currentThread().getName()+" "+ Thread.currentThread().getState()+" i = "+ i +"***");}}}}
第二种创建方式
创建一个实现了Runnable接口的类
实现类去实现Runnable接口中的抽象方法run()
创建实现类的对象
将此对象作为参数传递到Thraed类的构造器中,创建THread对象类
通过Thread类的对象调用start()方法
publicclassTest{publicstaticvoidmain(String[] args){
sub sub1 =newsub();newThread(sub1).start();/*
Thread的构造器
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
Thread的run方法
public void run() {
if (target != null) {
target.run();
}
}
*/}}classsubimplementsRunnable{@Overridepublicvoidrun(){for(int i =0; i <20; i++){
System.out.println("i = "+ i);}}}