Results 1 to 4 of 4
  1. #1
    vas
    vas is offline Member
    Join Date
    Jul 2014
    Posts
    7
    Rep Power
    0

    Default Efficient synchronization between threads

    I have 10 threads that represent agents in a virtual world.The world is divided into discrete blocks.An agent can move to any of these blocks if there isn't a wall or another agent.My problem is what happens when 2 agents try to move to the same block.I tried to used a synchronized block to sense decide and update the world ( for example agent senses the world, decides where to move, and then updates the world.No other agent can enter this region of code at the same time).
    This scheme results in sequential execution because each agent waits for the other to finish.Is any more efficient way to do this?
    I thought using locks each one for a different part of the world but i am not sure.Thanks in advance.

    Java Code:
    class Agent{
    public void pathfind() {
    		synchronized (World.lock) {
    			int[] data = sense();
    			for (int i = 0; i < 4; i++) {
    				if (data[i] == Empty)//no wall or agent
    				{	move(i);//moves the agent and updates the world
               World.Update();}
    			}
    		}
    	}
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Efficient synchronization between threads

    Quote Originally Posted by vas View Post
    I thought using locks each one for a different part of the world but i am not sure.
    What happened when you tried that?
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    vas
    vas is offline Member
    Join Date
    Jul 2014
    Posts
    7
    Rep Power
    0

    Default Re: Efficient synchronization between threads

    I haven't fully test it ,but it seems to be good considering that only adjacent agents need to to acquire a lock.

  4. #4
    vas
    vas is offline Member
    Join Date
    Jul 2014
    Posts
    7
    Rep Power
    0

    Default Re: Efficient synchronization between threads

    It seems faster but many times i have a deadlock.Let me ask something.If the thread has been interrupted when it calls wait() an interruptedException will be thrown.The thread proceeds to the catch block without blocking?In this case what happens with the lock?The thread still owns the lock?
    Java Code:
    synchronized (lock) {
    			while (true) {
    				try {
    					lock.wait();
    				} catch (InterruptedException e) {//interrupt handling
    				}
    			}
    		}

Similar Threads

  1. I need help in a synchronization threads
    By Alexis in forum Threads and Synchronization
    Replies: 0
    Last Post: 05-09-2013, 04:18 AM
  2. synchronization in java threads
    By naurinfarooq in forum New To Java
    Replies: 4
    Last Post: 07-23-2012, 07:33 AM
  3. Multiple Threads and Synchronization
    By yolo in forum Threads and Synchronization
    Replies: 6
    Last Post: 04-28-2012, 10:25 PM
  4. Threads and Synchronization
    By ASADUN in forum Threads and Synchronization
    Replies: 4
    Last Post: 12-18-2009, 07:00 AM
  5. Odd Even number synchronization in threads
    By DukeNukem in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-16-2009, 02:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •