Question2: I have almost same question but with Threads here is the code:
public class Example5 implements Runnable {
static int a=0;
public Example5() {
a++;
}
public int getA() {
return (a);
}
public void run() {
System.out.println(getA());
}
public static void main(String[] s) {
Example5 ex1 = new Example5();
new Thread(ex1).start();
Example5 ex2 = new Example5();
new Thread (ex2).start();
}
}
it says to me it must print: 1 or 2 2 anyone can explain this in threads.
Also anyone can explain to me what happen when i do for example(Not related to first question)
Thread t = new Thread();
t.start();
Thread t2 = new Thread();
t2.start();
Do they work sorted, (like first thread works first and then second thread) or it just goes randomly. thanks