Threads running problem, using flags

Asked

Viewed 374 times

6

I am having some problems in the complete understanding of the code because I am new to using java in thread. What code will do is control the flow, doing 55 iterations dividing between the thread and main program (main):

Resultado:
Main Thread.: 1
New Thread..: 2
Main Thread.: 3
New Thread..: 4
...
Main Thread.: 51
New Thread..: 52
Main Thread.: 53
New Thread..: 54

So far so good , the problem is , when in a certain execution the program enters in some infinite loop and does not stop to run again it completes the execution , wanted help to understand this problem.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 /
package testando;

/*
 *
 * @author Márcio
 */
public class Testando {
static int value = 0;
static int flag = 1;

@SuppressWarnings("empty-statement")
    public static void main(String[] args) {

                class MyThread extends Thread {

                        @Override
                        public void run() {
                                while(flag == 1);
                                while(++value < 55) {
                                        System.out.println("New Thread..: " + value);
                                        flag = 1;
                                        while( value < 55 && flag == 1);
                                }
                        }
                }

                Thread thread1 = new MyThread();
                thread1.start();
                System.out.println(flag == 0);
                while(flag == 0);
                while(++value < 55) {
                        System.out.println("Main Thread.: " + value);
                        flag = 0;
                        while( value < 55 && flag == 0);
                }
        }
    }
  • 1

    while(flag == 1); why this?

  • My was testing here, see the code run without this function, and also without while(flag == 0); , I ask you to disregard this part.

  • Could remove unnecessary tags within your question and code?

1 answer

4

What is causing racing conditions in its code is the fact that access to variables is not atomic, that is, they are concurrently accessed by more than one thread. If you want these variables to be used by the code without the operation performed in a thread affects the operation performed by the other, you must encapsulate the read and change operations of the variables in blocks synchronized (within which only one thread at a time) or then use objects that guarantee the atomicity of the operation as for example Atomicinteger.

  • That’s the problem, but I think AtomicInteger will not help in this case because the increment and the variable test happen in separate places.

  • True. But increment and test operations don’t have to be within the same block synchronized, only each of them should be atomic, no? The API of AtomicInteger maybe solve this, with methods such as compareAndSet(). Or this same API can allow the pair of actions themselves to be atomic.

  • I had not analyzed the code calmly. If it is rewritten properly the AtomicInteger probably solves it. The problem is that the code is a bit tricky, these loops "infinite" there to make synchronization do not smell good.

  • 1

    Ah, yes. Probably the OP needs a solution with wait() and notify()/notifyAll() to allow the threads change in the increment of the variable, dispensing with a flag control.

  • A Semaphore, one Countdownlatch or a Reentrantlock with two Conditions can also be used to avoid busy waiting and flag.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.