Synchronization Java Web Method

Asked

Viewed 61 times

2

I have a web application selling school canteen product, this process is accessed by several users simultaneously.. I have a method that updates and validates if quantity is available.. I need this method to be executed by one user at a time. I tried to put Synchronized but did not succeed, follow the excerpt:

public synchronized void atualizarQuantidadeItemCardapio(Cardapio itemCardapio) throws NegocioException {
    for (MovimentoCantina itemVenda : movimento) {
        if (itemCardapio.getProduto().getCodigo().equals(itemVenda.getProduto().getCodigo())) {
            validarProdutoEsgotado(itemCardapio);
            if (!estaVazio(itemCardapio.getQuantidade())) {
                itemCardapio = atualizarItemCardapio(itemCardapio);
                itemCardapio.setTransacao(persistencia);
                diminuirQuantidadeCardapio(itemCardapio, itemVenda);
                itemCardapio.preencherSituacao();
                itemCardapio.alterarQuantidade();
                itemCardapio.alterarVersao();
                criarLogCardapio(itemCardapio, itemVenda, CardapioLog.VENDA);
            }
        }
    }
}
  • There was some execution/compilation error ?

  • If I understand correctly, you just want to update the amount of items, if that is it, there is no need to be Synchronized. What you need is to know if it is available, right? You will have to perform the consultation before the purchase is completed.

  • Yes.. I am doing the query but as two users are doing the same query at the same.. they take the same amount and make the purchase. Example: I have a certain product whose quantity is 1.. both buy this product at the same time.. so when they perform the query they both check that it is 1 so it has available.. both finalize the sale but it was for one of them to receive the unavailable product error.

1 answer

1


Syncronized works in 3 different ways:

public synchronized void foo() { }

This version of foo() is an instance method, meaning different threads can invoke foo() simultaneously as long as the calls are performed in different instances. Lock is done in the instance where foo() has been invoked.

public static synchronized void foo() { }

This version of foo() can only be executed by one Thread at a time because the lock is done in the Class object of the type foo() has been invoked.

Finally Java still allows the use of Synchronized blocks:

public void foo() { synchronized(user) { .... } }

In this case, the execution policy will depend on the object passed in the block. If it is instance, the lock is done in the instance. If it is a static attribute, the lock is made on the Class object. It allows you greater freedom by allowing only a portion of the method to be synchronized.

I took this information literally from a question about Synchronized link I believe that your solution may be in the way you are using, as the description of the problem is too short it was not more objective.

  • I have two applications with different instances running this method at the same time, should I use public Static Synchronized void foo() { } ? Thank you

  • If you expect to create a kind of bottleneck, where each call awaits the end of the other call execution , yes.

Browser other questions tagged

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