Java 如何确定在ArrayList中一秒钟内添加的数据的大小?
我有一个ArrayList,其中数据是动态添加的Java 如何确定在ArrayList中一秒钟内添加的数据的大小?,java,arraylist,Java,Arraylist,我有一个ArrayList,其中数据是动态添加的 ArrayList<Double> al = new ArrayList<Double>(); while ( data !=null ){ al.add(data); } ArrayList al=new ArrayList(); while(数据!=null){ 添加(数据); } 问题是 1.如何在一秒钟内获得添加数据的大小? 2.如果实时添加数据,是否可以使用ArrayList? 因此,我尝试在没有真实数据
ArrayList<Double> al = new ArrayList<Double>();
while ( data !=null ){
al.add(data);
}
ArrayList al=new ArrayList();
while(数据!=null){
添加(数据);
}
问题是
1.如何在一秒钟内获得添加数据的大小?
2.如果实时添加数据,是否可以使用ArrayList?
因此,我尝试在没有真实数据的情况下用随机生成的数据替换。这就是我到目前为止所做的
static long startTime = System.currentTimeMillis();
static double sec = 5;
static double ans;
static double whenToEnd = 30;
static int i = 0;
int ii = 1;
static ArrayList<Double> al = new ArrayList<Double>();
private static int DataLengthIn1s;
static int previousLength = 0;
public static void main(String[] args) {
// TODO Auto-generated method stub
while ((System.currentTimeMillis() - startTime) < whenToEnd * 1000) {
writeData();
System.out.println("Current size of al: " + al.size());
}
System.out.println("Total size of al: " + al.size());
long endTime = System.currentTimeMillis();
long totalTime = (endTime - startTime);
System.out.println("Total process time: " + totalTime);
}
public static void writeData() {
long innerStartTime = System.currentTimeMillis();
while ((System.currentTimeMillis() - innerStartTime) < sec * 1000) {
ans = getRandom();
al.add(i, ans);
getDataLengthIn1s(innerStartTime);
i++;
}
}
private static int getDataLengthIn1s(long innerStartTime) {
if ((System.currentTimeMillis() + innerStartTime) == 1000) {
int length = al.size() - previousLength;
System.out.println("In one second, data added are: " + length);
previousLength = al.size();
}
return 0;
}
private static double getRandom() {
// TODO Auto-generated method stub
double lower = 0;
double upper = 1;
double result = Math.random() * (upper - lower) + lower;
try {
TimeUnit.MILLISECONDS.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
static long startTime=System.currentTimeMillis();
静态双秒=5;
静态双ans;
静态双whenToEnd=30;
静态int i=0;
int ii=1;
静态ArrayList al=新ArrayList();
私有静态int-DataLengthIn1s;
静态int-previousLength=0;
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
而((System.currentTimeMillis()-startTime)
在这段代码中,我在30秒内获得600个数据。表示600/30=20个数据/秒。但对于真正的实现,发送一秒钟的数据不会一直是相同的。另一个原因是我可以通过知道当前添加数据的大小来删除以前的数据。
提前谢谢
如何在一秒钟内获得添加数据的大小
如果您想知道在最后一秒钟有多少项已添加到列表中,则可以检查列表中当前有多少项,请稍等片刻,然后再次检查列表中有多少项。但是,请注意,由于等待时间取决于调度程序,因此此测量结果并不准确
在以下代码段中,一个线程每秒检查在最后一秒中添加到列表中的项目数:
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
public class ListPerformance {
public static void main(String[] args) {
final AtomicBoolean stop = new AtomicBoolean(false);
final List<Double> queue = new ArrayList<Double>();
Thread producer = new Thread(new Runnable() {
public void run() {
while (!stop.get()) {
queue.add(Math.random());
try {
Thread.sleep(Math.round(Math.random() * 10));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
Thread timer = new Thread(new Runnable() {
public void run() {
while (!stop.get()) {
int startingSize = queue.size();
long startingTime = System.currentTimeMillis();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Added " + (queue.size() - startingSize) + " items in " + (System.currentTimeMillis() - startingTime) + " ms");
}
}
});
producer.start();
timer.start();
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
stop.set(true);
}
}
请注意,该列表在两个线程之间共享。例如,如果您想在一个线程中从列表中删除项目,并在第二个线程中添加项目,那么您绝对必须在生产线程和消费线程中锁定列表,例如,synchronized(queue){…
。最有可能的是,还存在许多优化的线程安全列表实现
我不确定你的最终目标是什么,但是你也可以考虑每秒创建一个新的列表(我将它绑定到一个数据量而不是时间),并且有多个独立的数据集合,而不是一个巨大的集合。这样,你可以扔掉你不需要的整个列表而不是修改你的唯一列表。 如果实时添加数据,是否可以使用ArrayList
如果你在谈论一个真正的实时系统,那么我认为列表的实现不是你应该关心的。更重要的是线程和垃圾收集的调度。我不能告诉你如何实现实时系统,但是如果你考虑使用java。 如果您不是在谈论真正的实时,而只是想知道是否有更好的集合供您使用,那么-根据您的描述-您可能希望搜索Java缓冲区或Java队列,并决定是否找到适合您特定用例的内容
Added 190 items in 1000 ms
Added 193 items in 1000 ms
Added 208 items in 1000 ms
Added 200 items in 1000 ms
...