I have some problems when I test the time it takes to insert 10,000,000 elements into ArrayList and LinkedList. When I put the test code at both ends in two main() functions, the time spent on the LinkedList is greater than about ArrayList, which is about twice. 
When I put both ends of the code in the same main() method and the ArrayList is inserted first, the ArrayList takes longer than the LinkedList, which is about twice as large. I want to know what happened?
In theory, it should be that ArrayList takes less time.
The first piece of code:
public static void main(String[] args) {
    long begin2 = System.currentTimeMillis();
    List<Integer> l2 = new LinkedList<>();
    for (int i = 0; i < 10000000; i++) {
        l2.add(Integer.MAX_VALUE);
    }
    long end2 = System.currentTimeMillis();
    System.out.println(end2 - begin2); //Time: 12362
}
public static void main(String[] args) {
    long begin1 = System.currentTimeMillis();
    List<Integer> l1 = new ArrayList<>();
    for (int i = 0; i < 10000000; i++) {
        l1.add(Integer.MAX_VALUE);
    }
    long end1 = System.currentTimeMillis();
    System.out.println(end1 - begin1); //Time: 7531
}
Second piece of code:
public static void main(String[] args) {
    long begin1 = System.currentTimeMillis();
    List<Integer> l1 = new ArrayList<>();
    for (int i = 0; i < 10000000; i++) {
        l1.add(Integer.MAX_VALUE);
    }
    long end1 = System.currentTimeMillis();
    System.out.println(end1 - begin1); //Time: 7555
    long begin2 = System.currentTimeMillis();
    List<Integer> l2 = new LinkedList<>();
    for (int i = 0; i < 10000000; i++) {
        l2.add(Integer.MAX_VALUE);
    }
    long end2 = System.currentTimeMillis();
    System.out.println(end2 - begin2); //Time: 3533
 }
The next code makes me even more confused, because the time difference between the two pieces of code is not so obvious:
`public static void main(String[] args) {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long begin1 = System.currentTimeMillis();
        List<Integer> l1 = new ArrayList<>();
        for (int i = 0; i < 10000000; i++) {
            l1.add(Integer.MAX_VALUE);
        }
        long end1 = System.currentTimeMillis();
        System.out.println(end1 - begin1); //Time: 4542
        long begin2 = System.currentTimeMillis();
        List<Integer> l2 = new LinkedList<>();
        for (int i = 0; i < 10000000; i++) {
            l2.add(Integer.MAX_VALUE);
        }
        long end2 = System.currentTimeMillis();
        System.out.println(end2 - begin2); //Time: 4325
    }`
 
    