class InsertData {

public static int num =0;
private static Object lock = new Object(); // 创建一个对象作为锁

public void insert(Thread thread) {
	synchronized(lock){ // 加锁
		for (int i = 0; i <= 5; i++) {
			num++;
			System.out.println(num);
		}
	} // 解锁
}

}

编程要求请仔细阅读右侧代码根据方法内的提示在Begin - End区域内进行代码补充具体任务如下:使num变量在同一时刻只能有一个线程可以访问。测试说明使程序的输出结果如下:123456789101112131415161718package step2;public class Task 	public static void mainString args 				final InsertDa

原文地址: https://www.cveoy.top/t/topic/eDXr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录