Jump to content
  • 0

Question

Posted

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

9 answers to this question

Recommended Posts

  • 0
Posted

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

  • 0
Posted
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)

  • 0
Posted (edited)

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
  • 0
Posted

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 

  • 0
Posted

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

  • 0
Posted (edited)

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
  • 0
Posted

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

  • 0
Posted

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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Posts

    • Expanding the dominion, the magical Scroll of Escape   The months passed and Orcis dominance over Aden was absolute. Everyone respected his leadership and authority. Now many of his highly ranked soldiers have learned the boat dance of invulnerability but the army had trouble mastering it.    The orc ordered his most loyal engineer, that stranger who his name we still query, to find a way to teach the dance to the solders to turn them into super-soldiers. The stranger searched and search ed and searched, but he could not make the dance easier. For a soldier, with his heavy armor and slow movement was confined to the limits that this shell provided.   But sometimes, the truth hides in simpler things. One day, after testing the dance on the boat many times, the stranger decided to call it a day. But he was too tired to walk back to the city from the Giran Harbor. So he opened his back and used a scroll of escape while still on the boat.  He visited the weapons store, then the church and later decided to go to the west gate of Giran, to do some light sparring with the random villagers.    To his surprise, he could not fight the villagers. Whenever they tried to swing on him, their sword bounced away. And so it came to the strnager, that its not the dance that makes Einhasad bless him with immortality. It's not gods themselves, but an inherent flaw of the mortal world.    And so ... he went to the king, with a bright idea, and a bunch of Scrolls of Escape in hand.   Master I know of a way. And I know of a thousand other ways to trick the gods and gain immortality without dancing infront of them. For it is not the blessing of Einhasad that gives us the gift.    The gods have erred, the world is flawed and only a redesign of the nature's principles can now stop us ...     Epilogue Years ago, there have been stories of people spontaneously blessed by Einhasad. People who would walk on Primeval Isle or in Sieges and be immortal. The elders of the world, the wise ones, never took notice, never acted. For years and years, the ancient magic went unnoticed, but the stranger, figured it out. There's no magic. There never was. And the only reason why the wise ones never figured it out, was cause they had their "telescopes" pointed to the wrong direction. For it's the insignificant, the in-exceptional places, where "magic begins" (If you find my stories interesting, the next story will be about a Canvas in School of Dark Arts that overflows with Ink)
    • I just found it funny that you reversed the reverse engineer champions. They won't like this for sure.    Anyway, the project vision is very good it is aligned with what the players need and want something that both e-global and l2reborn has failed to provide. These are stable projects, they are not good "games". And players play "games" they don't play "projects".  When no good "games" exist, they flock towards good "projects". Lineage 2 private scene has produced decent projects the last years, but no decent "game". I hope you are the first ones to do so.   Generally there are two axis that Lineage 2 needs to evolve on. Modern gaming concepts and the need to respect the aging player's time. So far, most projects treat these aspects as taboo, they seem so afraid to deviate from what they call "true" Lineage 2. But the reality is that times change, you can't stay static forever, you have to evolve. NCSoft failed,they are not paving the way no more. It is us now, the collective community that have to pave our own pathways. WoW has went the same way, private projects took initiative because the company (while not failed) could not steer the game towards what players want and need in 2026.    For that reason I believe your project is going towards the right direction. The four keystones listed in the vision is what I believe is the community really needs but clearly doesn't understand it wants, because noone so far has give them a glimspe of what is beyond their retail-like garden.    I personally don't respect projects like reborn and e-global. As "games", they are simply trash, regardless of their high professionalism as endeavors. They lack innovation, they lack creativity, they are simply good software products,     
  • Topics

×
×
  • Create New...

Important Information

This community uses essential cookies to function properly. Non-essential cookies and third-party services are used only with your consent. Read our Privacy Policy and We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue..