Jump to content
  • 0

How Synchronized Actually Works?


Afou To Patisa

Question

I checked already stackoverflow but i wanted to make sure. 

If we have a class example  upon click button it give's 1 coin to a player.

 

If method "giveCoin" is not synchronized and 2 players click at the same time while code executed it get fucked right?

But if we use synchronized does the 2nd threat wait for 1st one to finish?

 

Ty

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

In most of actual scenarios, the datatype is enough to be concurrent. That means, in 90% of cases, the use of ConcurrentHashMap since it's the best concurrent containers.

 

About your coin exemple, all depends about coin. If it's a npc, it's not a trouble. If it's an exchange between players, it "has" to be synchronized (it could be using a container, such as AtomicBoolean).

 

The problem with synchronize is it blocks the entire object so you have to care about which object to lock (can be a shared Lock for example). You also have to care about deadlocks, aka 2 threads which wait each other forever.

 

http://tutorials.jenkov.com/java-concurrency/deadlock.html

Link to comment
Share on other sites

  • 0
public syncronized void giveCoin()
{
      // Some code
}

That's enoght for locking this method and block any execute in the same time from few threads.

 

 

No you didn't understand, i knwo how to use synchronize the question is this..

Does it set priority?

 

Example: if 2 players as i said click to get the coin will the 1st one get it (the 1st who clicked first) and then the 2nd one? right? No both at the same time. Code will executed first for player1 then for the next thread that wait (player2)

Link to comment
Share on other sites

  • 0

generally synchronized keyword is working to prevent a mess while multiple threads try write/read the same for example variable or a class that uses another class while have access in the same variable.

 

In your case for the same time y, will be a 'pause' between the players reward

Edited by melron
Link to comment
Share on other sites

  • 0

I'll be more specific Tryskell.

Let's do an example:

public class Item
{
    public static synchronized void giveCoin(L2PcInstance player)
    {
      //Some code that might need 1 second to be executed cause is huge (coin is example)
    }
}

and then to an L2NpcInstance we add

if (command.startsWith("get"))
{
  Item.giveCoin(activeChar);
}

So the question here is. If the "giveCoin" needs let's say an example 0.4 second to be executed, and another player click before 0.4 finish. 

What will happen? the 2nd player will get an error? His click will be executed after the 1st one? e.t.c 

Link to comment
Share on other sites

  • 0

generally synchronized keyword is working to prevent a mess while multiple threads try write/read the same for example variable or a class that uses another class while have access in the same variable.

 

In your case for the same time y, will be a 'pause' between the players reward

That what i wanted to hear. I want a safe priority.. Like 1000 players click on that button if the code need 0.2 sec to be executed all threads will wait by turn one by one instead of method crash

especially when method adjust an int value outside the method like this:

int value = 0;

public synchronized void giveCoin(blabla)
{ 
   //blabla
  value = 5;
  //blabla
  value = 0;
}

i want to avoid the case of the value being adjusted by 1000 player at the same time

Link to comment
Share on other sites

  • 0

If your method is visibly taking time (on a human point of view), you should rework it, because whatever you coded inside is probably terrible. And no, until the result of the first player action is needed for second player action, aka shared content like a shared timer, etc. Which once again can be solved with different behaviors (AtomicBoolean / AtomicInteger, etc)

Edited by Tryskell
Link to comment
Share on other sites

  • 0

If your method is visibly taking time (on a human point of view), you should rework it, because whatever you coded inside is probably terrible. And no, until the result of the first player action is needed for second player action, aka shared content like a shared timer, etc. Which once again can be solved with different behaviors (AtomicBoolean / AtomicInteger, etc)

that was an extreme example (the 0.4 sec) is just some complicated code that if it another tread bypass it (another player)

it would be a disaster

Link to comment
Share on other sites

  • 0

Still, even if it takes 10sec, from the moment values aren't shared between players and aren't edited in same time, there is no uses of synchronizing things.

 

There are less than 150 synchronized keywords on the whole sources, no needs to tell you you're overthinking the concept.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...