学习笔记(04):19年并发编程及原理视频培训教程入门到精通-并发编程的挑战之线程安全...

立即学习:https://edu.csdn.net/course/play/9827/208781?utm_source=blogtoedu

import java.util.concurrent.CountDownLatch;
public class test1 {
	private static int number;
	public static void insert(){
		number++;
	}
	public static void main(String[] args) {
		final CountDownLatch count=new CountDownLatch(10);
		for(int i=0;i<10;i++){
			new Thread(new Runnable(){
				public void run() {
					// TODO Auto-generated method stub
					for (int j = 0; j < 500; j++) {
						insert();
					}
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					count.countDown();
				}
			}
			).start();
		}
		while(true){
			if(count.getCount()==0){
				System.out.println(number);
				break;
			}
		}
	}
}
发布了7 篇原创文章 · 获赞 6 · 访问量 33

猜你喜欢

转载自blog.csdn.net/weixin_45831970/article/details/104358343