public class Main
{
public static void main(String[] args)
{
// 进行10次测试
for(int i = 0; i < 10; i++)
{
test();
}
}
public static void test()
{
// 用来测试的List
List<Object> list = new ArrayList<Object>();
// 线程数量(1000)
int threadCount = 1000;
// 用来让主线程等待threadCount个子线程执行完毕
CountDownLatch countDownLatch = new CountDownLatch(threadCount);
// 启动threadCount个子线程
for(int i = 0; i < threadCount; i++)
{
Thread thread = new Thread(new MyThread(list, countDownLatch));
thread.start();
}
try
{
// 主线程等待所有子线程执行完成,再向下执行
countDownLatch.await();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
// List的size
System.out.println(list.size());
}
}
class MyThread implements Runnable
{
private List<Object> list;
private CountDownLatch countDownLatch;
public MyThread(List<Object> list, CountDownLatch countDownLatch)
{
this.list = list;
this.countDownLatch = countDownLatch;
}
public void run()
{
// 每个线程向List中添加100个元素
for(int i = 0; i < 100; i++)
{
list.add(new Object());
}
// 完成一个子线程
countDownLatch.countDown();
}
}
99946 100000 100000 100000 99998 99959 100000 99975 100000 99996
List<Object> list = new ArrayList<Object>();
List<Object> list = new Vector<Object>();
100000 100000 100000 100000 100000 100000 100000 100000 100000 100000
public class Main
{
public static void main(String[] args)
{
// 进行10次测试
for(int i = 0; i < 10; i++)
{
test();
}
}
public static void test()
{
// 计数器
Counter counter = new Counter();
// 线程数量(1000)
int threadCount = 1000;
// 用来让主线程等待threadCount个子线程执行完毕
CountDownLatch countDownLatch = new CountDownLatch(threadCount);
// 启动threadCount个子线程
for(int i = 0; i < threadCount; i++)
{
Thread thread = new Thread(new MyThread(counter, countDownLatch));
thread.start();
}
try
{
// 主线程等待所有子线程执行完成,再向下执行
countDownLatch.await();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
// 计数器的值
System.out.println(counter.getCount());
}
}
class MyThread implements Runnable
{
private Counter counter;
private CountDownLatch countDownLatch;
public MyThread(Counter counter, CountDownLatch countDownLatch)
{
this.counter = counter;
this.countDownLatch = countDownLatch;
}
public void run()
{
// 每个线程向Counter中进行10000次累加
for(int i = 0; i < 10000; i++)
{
counter.addCount();
}
// 完成一个子线程
countDownLatch.countDown();
}
}
class Counter
{
private int count = 0;
public int getCount()
{
return count;
}
public void addCount()
{
count++;
}
}
9963727 9973178 9999577 9987650 9988734 9988665 9987820 9990847 9992305 9972233
class Counter
{
private int count = 0;
public int getCount()
{
return count;
}
public synchronized void addCount()
{
count++;
}
}
10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有