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

    • Tell a Škoda 1.4 driver that a Škoda 1.8 is faster — and suddenly, you’re riding a mini race car. On the way, they might share random stories — gas prices, the perfect BBQ recipe, or how passengers once called them “just for a minute.” Maybe even how someone left a suitcase full of money in their car once.     The important part? You’ll get there on time, no delays.     Vibe SMS works just as fast — messages fly like that driver who just heard their 1.8 isn’t the fastest.     🌐 https://vibe-sms.net/ 📲 https://t.me/vibe_sms  
    • ⚔️ The Grand Opening Has Arrived! ⚔️ In just a few hours the gate to the eternal battlefield will be open and the war between Order and Chaos will be set once again ! Its time to claim your destiny 🔥 👉 Register now and join the fight today! 🌐 https://l2ovc.com register now : https://l2ovc.com The gates are open the war between Order and Chaos has officially started! 🔥 Join the battlefield NOW and claim your destiny in Order vs Chaos! 💥 Don’t fall behind your faction needs you. ➡️ https://l2ovc.com  
    • Don’t miss the new Telegram gifts with our Telegram Stars purchasing bot! A great opportunity to invest in a stable digital asset at an early stage while the market is still forming. Buy other existing gifts in the official store using Telegram Stars, pay for subscriptions, donate to games and projects, pay for Premium subscriptions, and react to messages in channels! Low prices, multiple payment options, and other cool unique features! ⚡ Try it today — SOCNET STARS BOT ⚡ Active links to SOCNET stores: Digital Goods Store (Website): Go Store Telegram Bot: Go – convenient access to the store via Telegram messenger. ⭐ Telegram Stars Purchase Bot: Go – fast and profitable way to buy stars in Telegram. SMM Panel: Go – promote your social media accounts. We present to you the current list of promotions and special offers for purchasing our products and services: 1️⃣ Promo code OCTOBER2025 (8% discount) for purchases in our store (Website, bot) in October! You can also use the promo code SOCNET (15% discount) for your first purchase. 2️⃣ Get $1 on your store balance or a 10–20% discount — just write your username after registration on our website using the template: "SEND ME BONUS, MY USERNAME IS..." — post it in our forum thread! 3️⃣ Get $1 for your first SMM Panel trial — simply open a ticket titled “Get Trial Bonus” on our website (Support). 4️⃣ Weekly ⭐ Telegram Stars giveaways in our Telegram channel and in our Telegram Stars bot! News: ➡ Telegram Channel: https://t.me/accsforyou_shop ➡ WhatsApp Channel: https://chat.whatsapp.com/K8rBy500nA73z27PxgaJUw?mode=ems_copy_t ➡ Discord Server: https://discord.gg/y9AStFFsrh Contacts and Support: ➡ Telegram: https://t.me/socnet_support ➡ WhatsApp: https://wa.me/79051904467 ➡ Discord: socnet_support ➡ ✉ Email: solomonbog@socnet.store
    • Don’t miss the new Telegram gifts with our Telegram Stars purchasing bot! A great opportunity to invest in a stable digital asset at an early stage while the market is still forming. Buy other existing gifts in the official store using Telegram Stars, pay for subscriptions, donate to games and projects, pay for Premium subscriptions, and react to messages in channels! Low prices, multiple payment options, and other cool unique features! ⚡ Try it today — SOCNET STARS BOT ⚡ Active links to SOCNET stores: Digital Goods Store (Website): Go Store Telegram Bot: Go – convenient access to the store via Telegram messenger. ⭐ Telegram Stars Purchase Bot: Go – fast and profitable way to buy stars in Telegram. SMM Panel: Go – promote your social media accounts. We present to you the current list of promotions and special offers for purchasing our products and services: 1️⃣ Promo code OCTOBER2025 (8% discount) for purchases in our store (Website, bot) in October! You can also use the promo code SOCNET (15% discount) for your first purchase. 2️⃣ Get $1 on your store balance or a 10–20% discount — just write your username after registration on our website using the template: "SEND ME BONUS, MY USERNAME IS..." — post it in our forum thread! 3️⃣ Get $1 for your first SMM Panel trial — simply open a ticket titled “Get Trial Bonus” on our website (Support). 4️⃣ Weekly ⭐ Telegram Stars giveaways in our Telegram channel and in our Telegram Stars bot! News: ➡ Telegram Channel: https://t.me/accsforyou_shop ➡ WhatsApp Channel: https://chat.whatsapp.com/K8rBy500nA73z27PxgaJUw?mode=ems_copy_t ➡ Discord Server: https://discord.gg/y9AStFFsrh Contacts and Support: ➡ Telegram: https://t.me/socnet_support ➡ WhatsApp: https://wa.me/79051904467 ➡ Discord: socnet_support ➡ ✉ Email: solomonbog@socnet.store
  • Topics

×
×
  • Create New...

AdBlock Extension Detected!

Our website is made possible by displaying online advertisements to our members.

Please disable AdBlock browser extension first, to be able to use our community.

I've Disabled AdBlock