Learning Java, why didn't I get some threads overlapping?
I tried the following code:
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
static int i = 0;
public static void main(String[] args) {
new Thread(t1).start();
new Thread(t2).start();
new Thread(t3).start();
new Thread(t4).start();
new Thread(t5).start();
new Thread(t6).start();
}
private static void countMe(String name){
i++;
System.out.println("Current Counter is: " + i + ", updated by: " + name);
}
private static Runnable t1 = new Runnable() {
public void run() {
try{
for(int i=0; i<2; i++){
countMe("t1");
}
} catch (Exception e){}
}
};
private static Runnable t2 = new Runnable() {
public void run() {
try{
for(int i=0; i<2; i++){
countMe("t2");
}
} catch (Exception e){}
}
};
private static Runnable t3 = new Runnable() {
public void run() {
try{
for(int i=0; i<2; i++){
countMe("t3");
}
} catch (Exception e){}
}
};
private static Runnable t4 = new Runnable() {
public void run() {
try{
for(int i=0; i<2; i++){
countMe("t4");
}
} catch (Exception e){}
}
};
private static Runnable t5 = new Runnable() {
public void run() {
try{
for(int i=0; i<2; i++){
countMe("t5");
}
} catch (Exception e){}
}
};
private static Runnable t6 = new Runnable() {
public void run() {
try{
for(int i=0; i<2; i++){
countMe("t6");
}
} catch (Exception e){}
}
};
}
and on the ideon I got the result:
Current Counter is: 1, updated by: t1
Current Counter is: 2, updated by: t1
Current Counter is: 3, updated by: t2
Current Counter is: 4, updated by: t2
Current Counter is: 5, updated by: t3
Current Counter is: 6, updated by: t3
Current Counter is: 7, updated by: t4
Current Counter is: 8, updated by: t4
Current Counter is: 9, updated by: t5
Current Counter is: 10, updated by: t5
Current Counter is: 11, updated by: t6
Current Counter is: 12, updated by: t6
It seemed that everything was going in a linear way, these are streams called the countMe function one by one, in the order in which I created them. Doesn't mean multiple threads mean they might fail. What am I missing here? Is this the case where the machine I'm running on (I tried at ideone.com) is configured in such a way that it spawns threads so that they are created?
source to share
Creating a theme is expensive. What is likely to happen is that by the time thread 2 ends, thread 1 has already finished. When thread 3 starts doing its thing, thread 2 is already finished. Etc.
Insert a six-way circular barrier at the start of the flow function and watch their race (maybe even losing increments i
, since it's i++
not guaranteed to be atomic).
If that's not enough to reliably start a race, make streams more useful.
source to share
It takes time to get started. They can take milliseconds or hundreds of microseconds. If that doesn't sound like a long time, consider that your processor can execute a million instructions in that time. those. your loops end before the next thread comes in.
A simple way to see this is: a) have a longer loop like 10+, and b) add a delay like Thread.sleep (1000); and you can see threads running at the same time.
I also suggest using AtomicInteger instead int
as i++
it is not thread safe.
source to share