提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
通过将代码放入finally子句中,可以确保执行代码块。
//来 自 n o w j a v a . c o m import java.io.FileOutputStream; import java.util.Random; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class Main { static Lock myLock = new ReentrantLock(); static Random random = new Random(); public static void main(String[] args) { myLock.lock(); try { int number = random.nextInt(5); int result = 100 / number; System.out.println("A result is " + result); try (FileOutputStream file = new FileOutputStream("file.out")) { file.write(result); } /*