[SOLVED] Synchronized code help

Discussion in 'Plugin Development' started by Squish000, Jun 18, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Squish000

    Ok i've got this error

    Code:
     2012-06-18 09:39:44 [WARNING] Could not properly handle event CREATURE_SPAWN: 
     java.lang.IllegalAccessError:  Synchronized code got accessed from another thread: 
     Squish000.MagicalWands.Spells.Spell at org.bukkit.event.Listener.onCreatureSpawn(Listener:0) 
     at sun.reflect.GeneratedMethodAccessor84.invoke(Unknown Source) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
     at java.lang.reflect.Method.invoke(Unknown Source)
     at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) 
     at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
     at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:460)
     at org.bukkit.craftbukkit.event.CraftEventFactory.callCreatureSpawnEvent(CraftEventFactory.java:226) 
     at net.minecraft.server.World.addEntity(World.java:890) 
     at org.bukkit.craftbukkit.CraftWorld.spawn(CraftWorld.java:959) 
     at org.bukkit.craftbukkit.CraftWorld.spawn(CraftWorld.java:782)
     at org.bukkit.craftbukkit.CraftWorld.spawnCreature(CraftWorld.java:335)
     at Squish000.MagicalWands.Spells.SpellSunSlash.start1(SpellSunSlash.java:48)
     at Squish000.MagicalWands.Spells.Spell.run(Spell.java:66) 2012-06-18 09:39:44
     [INFO] This error is logged only once: it could have occurred multiple times by now.
    
    I think its caused by two threads accessing the same function at the same time (spawning a creature), but im not sure. Could someone verify this please?
    And also if this is the case does anybody know a simple solution?
     
  2. Offline

    ItsHarry

    You expect people to help you if you don't even say what you're trying to do?
    Atleast tell us what you're trying to do and show us some source code..
     
  3. Offline

    Squish000

    The soruce code it uses stretches over 4 java files, basically it creates a thread that will do the spell on the world (like damage units around the target)

    Heres the soruce code for the spell its using
    Show Spoiler

    Code:
    package Squish000.MagicalWands.Spells;
    
    
    import java.util.List;
    
    import org.bukkit.Effect;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    
    
    public class SpellSunSlash  extends Spell{
        
    
        private Location target;
        private final double size;
        private List<Entity> b;
    
    
        public static int potionID = 21;
        public SpellSunSlash(World w,Location target,Player sender,double size)
        {
        super(w,sender);
        this.target=target;
        this.size =size;
        }
    
        
        
        public void tick()
        {
            
        }
    
    
        
        public void finish1() 
        {
        
        }
    
    
    
        public void start1() {
            LivingEntity a = getWorld().spawnCreature(getTarget(), EntityType.COW);
            a.remove();
            b = a.getNearbyEntities(3, 3, 3);
            for(int i = 0;i<b.size();i++)
                {
                if(b.get(i) != getSender() && b.get(i) instanceof LivingEntity)
                    {
                    if(getWorld().getTime() % 24000 <= 12000)//day
                    {
                    LivingEntity e = (LivingEntity) b.get(i);
                    e.damage((int) (size*2));
                    getWorld().playEffect(e.getEyeLocation(), Effect.MOBSPAWNER_FLAMES, 2);
                    getWorld().playEffect(e.getEyeLocation(), Effect.MOBSPAWNER_FLAMES, 1);
                    getWorld().playEffect(e.getEyeLocation(), Effect.MOBSPAWNER_FLAMES, 3);
                    }else
                    {//night
                        LivingEntity e = (LivingEntity) b.get(i);
                        e.damage((int) size);
                    }
                    }
                }
            setRunning(false);
            getSender().getWorld().playEffect(getSender().getEyeLocation().add(0.5, 0, 0.5), Effect.POTION_BREAK, potionID);
            getSender().getWorld().playEffect(getSender().getEyeLocation().add(0.5, 0, 0.5), Effect.POTION_BREAK, potionID);
        }
    
        
        
        
        
    
    
    
    
        public Location getTarget() {
            return target;
        }
    
    
    
        public void setTarget(Location target) {
            this.target = target;
        }
    
    
    
    
        public double getSize() {
            return size;
        }
        
        
        
        
        
        
        
        
        
        
    }
    
     
  4. Offline

    ItsHarry

    So don't use a thread, if you know two threads are accessing the same function, then why do you still make a new thread? Just keep it on the main thread..
     
  5. Offline

    Squish000

    The threads don't do exactly the same thing, the target, sender and size can vary. So it could be running two threads but the effects of the thread occur in different parts of the world.
     
  6. Offline

    ItsHarry

    Yeah but you can't just edit something which is running on a different thread and expect it to work, you will get sync issues.
     
  7. Offline

    Squish000

    Thats what i established in the first post , do you have any solutions to that problem?
     
  8. Offline

    ItsHarry

    Like I said, instead of using a thread, keep the code on the main thread.
     
  9. Offline

    Korvacs

    Use the synchronised keyword on whichever object it is which is causing the issue.
     
  10. Offline

    Squish000

    But then how would you run multiple instances of it, without using threads?
     
  11. Offline

    ItsHarry

    O__O.
    You don't need a thread to make a new instance O___________OOOOO""""
     
  12. Offline

    Squish000

    nvm, i don't think you quite understood the question i asked....

    Thanks, that solved it :D
     
  13. Offline

    Sleaker

    Squish000 - basically you need to figure out a way to not use seperate threads to run your code. bukkit has a synchronized scheduler if you need to schedule operations to happen at later dates. Other than this, you may need to dive back into some basic java refreshing/learning on how Objects work.

    There is absolutely no reason to attempt running your code on a seperate thread. You shouldn't be touching synchronization, especially if you're trying to make things happen in the world or are altering players etc.
     
  14. Offline

    Squish000

    Thanks, that cleared it up a bit, its just a lot more simple to make the spells run on threads instead of creating and updating them as objects constantly. Although, i will probably switch to the objects version if any more bugs occur or it causes lag.
    Also what are the reason why threads and synchronization are bad?
     
  15. Offline

    Korvacs

    There's nothing bad about using threads or synchronization in your code, just as long as you know what your doing, and so long as what your doing justifies the use of a thread.

    In this case the code you were trying to run in a thread referenced an object which was locked by another thread, the only way to access this code 'legally' is to have your thread also attempt to lock the object, the locking will then alternate between the various threads attempting to lock the object. The downside of doing this is that you block the execution on the other threads when you take the lock, however this shouldn't be a problem so long as you maintain the lock on the object for the smallest possible time.

    Conceivably the scheduler would be preferred so long as you can fit your code to work with it, simply because this is basically a thread which performs small tasks on a single thread which is more efficient than a thread for each small task.
     
Thread Status:
Not open for further replies.

Share This Page