1

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...?

  • 1
    I'm not entirely sure about android, but I would find it hard to believe that the button clicks are handled asynchronously, so I doubt that the threads aren't being started in the correct order, but rather, their execution order isn't guaranteed. It would probably be better to use an ExecutorService with a single backing thread or similar. http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html – Mark W Jan 26 '15 at 17:34
  • 1
    Where and how do you declare your lock `contadordeMierda`? – iForests Jan 26 '15 at 17:41
  • inside run(){ synchronized ((Object) contadordeMierda){... if i dont cast to Object it shows an error – Cristian Marin Jan 26 '15 at 20:41
  • Did you declare `contadordeMierda` inside your button's OnClickListerner? – iForests Jan 27 '15 at 02:37

2 Answers2

1

This is the exact purpose of join() function. A many times asked question: How to wait for a number of threads to complete?.

Community
  • 1
  • 1
logoff
  • 3,347
  • 5
  • 41
  • 58
  • how can i use it? and how can i guarantee that int will have the correct value? – Cristian Marin Jan 26 '15 at 20:44
  • in both links is explained how to use it. you can not guarantee your order if you initialize the `int` inside the thread. put your int outside the scope of the thread or pass it to the thread at the beginning. – logoff Jan 27 '15 at 08:24
-1

Why not do it like this: you let a new thread be started only if there's no thread currently running:

private boolean isThreadRunning = false;//declare a boolean field to store thread status whether it's running or not
...

if(!isThreadRunning){
    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);
                isThreadRunning = false;//Set the boolean variable to false because the thread has completed it's operations.
            }
   }
    });

    a.start();
    isThreadRunning = true;// Set the boolean to true because thread.start has been called.

}
ayz4sci
  • 2,220
  • 1
  • 16
  • 17