线程顺序执行的四种方式
1、通过监测信号 每个线程对应自己的一个信号,监测到信号就执行,执行完就将信号更改为下一个线程的信号 2、通过FutureTask可以阻塞获取返回值的特性 顺序开启线程并获取返回值,线程将依次执行,返回结果 3、通过join()阻塞等待线程执行完 按线程执行顺序,依次join()进行阻塞 4、通过单线程线程池特性 线程池中只有一个线程,任务会在队列中,按提交顺序依次执行
线程顺序执行的代码示例
public class ThreadOrder {
public static List< Integer> list = new ArrayList < > ( ) ;
public static void main ( String[ ] args) {
Thread t11 = new Thread ( "通过监测信号 : 0" ) {
@Override
public void run ( ) {
while ( list. get ( 0 ) != 0 ) {
try { Thread. sleep ( 500 ) ; } catch ( Exception ignore) { }
}
System. out. println ( super . getName ( ) + " : 执行任务" ) ;
list. remove ( 0 ) ;
}
} ;
Thread t12 = new Thread ( "通过监测信号 : 1" ) {
@Override
public void run ( ) {
while ( list. get ( 0 ) != 1 ) {
try { Thread. sleep ( 500 ) ; } catch ( Exception ignore) { }
}
System. out. println ( super . getName ( ) + " : 执行任务" ) ;
list. remove ( 0 ) ;
}
} ;
Thread t13 = new Thread ( "通过监测信号 : 2" ) {
@Override
public void run ( ) {
while ( list. get ( 0 ) != 2 ) {
try { Thread. sleep ( 500 ) ; } catch ( Exception ignore) { }
}
System. out. println ( super . getName ( ) + " : 执行任务" ) ;
list. remove ( 0 ) ;
}
} ;
list. add ( 0 ) ;
list. add ( 1 ) ;
list. add ( 2 ) ;
t11. start ( ) ;
t12. start ( ) ;
t13. start ( ) ;
FutureTask< String> task1 = new FutureTask < > ( new Callable < String> ( ) {
@Override
public String call ( ) throws Exception {
System. out. println ( Thread. currentThread ( ) . getName ( ) + " : 执行任务" ) ;
return Thread. currentThread ( ) . getName ( ) + " : 返回值" ;
}
} ) ;
Thread t21 = new Thread ( task1, "通过FutureTask : 0" ) ;
FutureTask< String> task2 = new FutureTask < > ( new Callable < String> ( ) {
@Override
public String call ( ) throws Exception {
System. out. println ( Thread. currentThread ( ) . getName ( ) + " : 执行任务" ) ;
return Thread. currentThread ( ) . getName ( ) + " : 返回值" ;
}
} ) ;
Thread t22 = new Thread ( task2, "通过FutureTask : 1" ) ;
FutureTask< String> task3 = new FutureTask < > ( new Callable < String> ( ) {
@Override
public String call ( ) throws Exception {
System. out. println ( Thread. currentThread ( ) . getName ( ) + " : 执行任务" ) ;
return Thread. currentThread ( ) . getName ( ) + " : 返回值" ;
}
} ) ;
Thread t23 = new Thread ( task3, "通过FutureTask : 2" ) ;
t21. start ( ) ;
try { System. out. println ( task1. get ( ) ) ; } catch ( Exception ignore) { }
t22. start ( ) ;
try { System. out. println ( task2. get ( ) ) ; } catch ( Exception ignore) { }
t23. start ( ) ;
try { System. out. println ( task3. get ( ) ) ; } catch ( Exception ignore) { }
Thread t31 = new Thread ( "通过join()阻塞 : 0" ) {
@Override
public void run ( ) {
try { Thread. sleep ( 1000 ) ; } catch ( Exception ignore) { }
System. out. println ( super . getName ( ) + " : 执行任务" ) ;
}
} ;
Thread t32 = new Thread ( "通过join()阻塞 : 1" ) {
@Override
public void run ( ) {
try { Thread. sleep ( 1000 ) ; } catch ( Exception ignore) { }
System. out. println ( super . getName ( ) + " : 执行任务" ) ;
}
} ;
Thread t33 = new Thread ( "通过join()阻塞 : 2" ) {
@Override
public void run ( ) {
try { Thread. sleep ( 1000 ) ; } catch ( Exception ignore) { }
System. out. println ( super . getName ( ) + " : 执行任务" ) ;
}
} ;
t31. start ( ) ;
try { t31. join ( ) ; } catch ( Exception ignore) { }
t32. start ( ) ;
try { t32. join ( ) ; } catch ( Exception ignore) { }
t33. start ( ) ;
try { t33. join ( ) ; } catch ( Exception ignore) { }
ExecutorService pool = Executors. newSingleThreadExecutor ( ) ;
Thread t41 = new Thread ( "通过单线程线程池 : 0" ) {
@Override
public void run ( ) {
try { Thread. sleep ( 1000 ) ; } catch ( Exception ignore) { }
System. out. println ( super . getName ( ) + " : 执行任务" ) ;
}
} ;
Thread t42 = new Thread ( "通过单线程线程池 : 1" ) {
@Override
public void run ( ) {
try { Thread. sleep ( 1000 ) ; } catch ( Exception ignore) { }
System. out. println ( super . getName ( ) + " : 执行任务" ) ;
}
} ;
Thread t43 = new Thread ( "通过单线程线程池 : 2" ) {
@Override
public void run ( ) {
try { Thread. sleep ( 1000 ) ; } catch ( Exception ignore) { }
System. out. println ( super . getName ( ) + " : 执行任务" ) ;
}
} ;
pool. submit ( t41) ;
pool. submit ( t42) ;
pool. submit ( t43) ;
pool. shutdown ( ) ;
}
}