Runnable task5 = new Runnable() { public void run() { try { System.out.println("采集 start"); runCase(suite + "datacollection/Test-Dg-All-TestSuite.xml"); Thread.sleep(1000); System.out.println("task5 end"); } catch (InterruptedException e) { System.out.println("task5 interrupted: " + e); } } };优化一下这段代码
时间: 2023-11-28 08:48:42 浏览: 49
线程Runnable的实现
可以使用 lambda 表达式来简化这段代码,如下所示:
```
Runnable task5 = () -> {
try {
System.out.println("采集 start");
runCase(suite + "datacollection/Test-Dg-All-TestSuite.xml");
Thread.sleep(1000);
System.out.println("task5 end");
} catch (InterruptedException e) {
System.out.println("task5 interrupted: " + e);
}
};
```
使用 lambda 表达式可以使代码更加简洁易读,同时还可以减少冗余的代码。
阅读全文