多线程实现
Thread类实现
public class Thread1 extends Thread {
@Override
public void run ( ) {
for ( int i = 0 ; i < 20 ; i++ ) {
System. out. println ( "多线程" + i) ;
}
}
}
调用
public class Thread1Main {
public static void main ( String[ ] args) {
Thread1 thread1 = new Thread1 ( ) ;
thread1. start ( ) ;
for ( int i = 0 ; i < 200 ; i++ ) {
System. out. println ( "主线程" + i) ;
}
}
}
Runnable接口实现
public class Thread2 implements Runnable {
@Override
public void run ( ) {
for ( int i = 0 ; i < 200 ; i++ ) {
System. out. println ( "多线程" + i) ;
}
}
}
调用
public class Thread2Main {
public static void main ( String[ ] args) {
Thread2 thread2 = new Thread2 ( ) ;
new Thread ( thread2) . start ( ) ;
for ( int i = 0 ; i < 20 ; i++ ) {
System. out. println ( "主线程" + i) ;
}
}
}
建议使用Runnable接口,,如果继承Thread,可能会有OOP单继承局限性