This code is called every time you click a button
//Thread called when click a button
Thread a = new Thread(new Runnable() {
@Override
public void run() {
synchronized ((Object) contadordeMierda){
Random rn = new Random();
int n= rn.nextInt(10) + 1;
contador++;
try {
Thread.sleep(n*100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(contador);
}
}
});
a.start();
When i touch it several times fast i get this outprint:
I/System.out﹕ 1
I/System.out﹕ 2
I/System.out﹕ 5
I/System.out﹕ 5
I/System.out﹕ 9
I/System.out﹕ 9
I/System.out﹕ 9
I/System.out﹕ 9
I/System.out﹕ 9
...
How can i do to wait for one thread to finish to start another one? So the print goes 1 2 3 4 5 6 7 8 9 10 11 12...?