I'm playing around with threads in Java for the first time and I'm just trying to convince myself they do what I think they are doing.
I expect the following code to run two loops at the same time, therefore I'm expecting the output to be some a mix of the counters. However, every time I run the code I get a straight count from 1 to 3000 with all the numbers in sequence.
Am I missing something? Is there a better way to demonstrate two threads actually working at the same time?
public class ThreadDemo {
public static void main(String[] args) {
Loop1 loop1 = new Loop1();
Loop2 loop2 = new Loop2();
loop1.run();
loop2.run();
}
public static class Loop1 implements Runnable {
@Override
public void run() {
for(int i= 1; i <= 1000; i++){
System.out.println(i);
}
}
public static class Loop2 implements Runnable {
@Override
public void run() {
for(int i= 2000; i <= 3000; i++){
System.out.println(i);
}
}
}