具有CompletableFuture的Java 8线程:在supplyAsync()之后关闭?

时间:2019-04-23 18:09:09

标签: java

假设我正在如下运行异步线程,

ExecutorService

是否需要执行任何关闭操作以防止此块后发生内存泄漏?当我使用service.shutdown()时,我不得不做class Home extends StatefulWidget { @override _HomeState createState() => new _HomeState(); } class _HomeState extends State<Home> { final List<String>listof=["Decimal Numbers","Binary Numbers","Decimal-to-Binary Conversion","Binary Arithmetic","Complements of Binary Numbers","Signed Numbers","Arithmetic Operations with Signed Numbers","Hexadecimal Numbers","Octal Numbers","Binary Coded Decimal (BCD)"]; @override Widget build(BuildContext context) { return new Scaffold( appBar: new AppBar( title: new Text( "Computer System Theory", style: new TextStyle(fontSize: 19.0), ), backgroundColor: Colors.deepPurple, actions: <Widget>[ new IconButton( icon: new Icon(Icons.search), onPressed: ()=>debugPrint("Search"), ), ], ), body: new Container( child: new ListView.builder( itemBuilder: (_, int index)=>listDataItem(this.listof[index],), itemCount: this.listof.length, ), ), ); } } class listDataItem extends StatelessWidget{ String itemName; listDataItem(this.itemName); @override Widget build(BuildContext context){ return new Card( elevation: 7.0, child: new Container( margin: EdgeInsets.all(5.0), padding: EdgeInsets.all(6.0), child: new Row( children: <Widget> [ new CircleAvatar( child: new Text(itemName[0]), backgroundColor: Colors.deepPurple, foregroundColor: Colors.white, ), new Padding(padding: EdgeInsets.all(8.0)), new Text(itemName,style: TextStyle(fontSize: 18.0)), ], ), ),); } }

1 个答案:

答案 0 :(得分:5)

不。如果您没有为supplyAsync提供执行程序,那么它将使用fork-join common pool,而不必启动或停止该执行程序。实际上,它不能被停止。

  

此池是静态构造的;其运行状态不受尝试shutdown()shutdownNow()的影响。