一种方式:
class MyRunnable implements Runnable {
@Override
public void run ( ) {
for ( int i = 0 ; i < 10 ; i++ ) {
System . out. println ( "线程正在执行 -> " + i) ;
try {
TimeUnit . SECONDS . sleep ( 1 ) ;
} catch ( InterruptedException e) {
throw new RuntimeException ( e) ;
}
}
}
}
Thread thread = new Thread ( new MyRunnable ( ) ) ;
thread. start ( ) ;
另一种方式
Runnable runnable = new Runnable ( ) {
public void run ( ) {
for ( int i = 0 ; i < 10 ; i++ ) {
System . out. println ( "线程正在执行 -> " + i) ;
try {
TimeUnit . SECONDS . sleep ( 1 ) ;
} catch ( InterruptedException e) {
throw new RuntimeException ( e) ;
}
}
}
} ;
Thread thread = new Thread ( runnable) ;
thread. start ( ) ;
其二二方式
Thread thread = new Thread ( new Runnable ( ) {
public void run ( ) {
for ( int i = 0 ; i < 10 ; i++ ) {
System . out. println ( "线程正在执行 -> " + i) ;
try {
TimeUnit . SECONDS . sleep ( 1 ) ;
} catch ( InterruptedException e) {
throw new RuntimeException ( e) ;
}
}
}
} ) ;
thread. start ( ) ;
}