Results 1 to 2 of 2
  1. #1
    JavaCoder is offline Member
    Join Date
    Aug 2010
    Posts
    1
    Rep Power
    0

    Question Leave thread yield or attempt join... or something else?

    I have a rather complex multi-threading problem I have solved but I'm not sure if I solved it the best way possible. I have a a java service that receives messages and stores them in Oracle by executing a stored procedure. The problem stems from two or more threads processing
    messages dealing with the trade calling the stored procedure at the same time. So if two threads are dealing with the same trade info we want them to update the database synchronously. Synchronous calls to the database alleviates unwanted locking in the DB. In the code contextIdentifier uniquely identifies what trade a message is for.

    Java Code:
    	private ConcurrentMap<String, Boolean> tradeLockMap = new ConcurrentHashMap<String, Boolean>();
    
    	In method that processes message...
    
    		// Context key to identify trade
    		String contextIdentifier = message.getContextIdentifier();
    		try {
    			while ([COLOR="Red"]![/COLOR]isLockAquired(contextIdentifier)) {
    				Thread.yield();
    			}
    			execStatement(statementCreator);
    		} finally {
    			tradeLockMap.remove(contextIdentifier);
    		}
    Method that locks trades so messages dealing with same trade are processed synchronously

    Java Code:
    	private boolean isLockAquired(String contextIdentifier) {
    		Boolean isTradeLocked = tradeLockMap.putIfAbsent(contextIdentifier, Boolean.TRUE);
    		if (isTradeLocked != null) {
    			return false;
    		}
    		return true;
    	}
    We used to have terrible database performance before adding this code in the java service but we still have a huge variance in the number of messages we push through per minute. We run 30 threads which are configured in our messaging system. I was thinking that joining the threads would be
    the best possible solution to the problem but I'm not sure how to do that since the code doesn't spawn the thread I want to join. I'm not even sure if doing a join would result in any performance gain. Thoughts???

    Thanks!
    Last edited by Fubarable; 08-11-2010 at 06:34 PM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Moderator edit: code tags added.

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Good luck and welcome to the forum!

Similar Threads

  1. calling yield() method in synchronized block
    By Ash-infinity in forum New To Java
    Replies: 2
    Last Post: 12-04-2012, 05:35 PM
  2. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 05:37 PM
  3. First TicTacToe Attempt
    By Pyrexkidd in forum New To Java
    Replies: 2
    Last Post: 07-25-2010, 09:32 PM
  4. Join, basic question about thread
    By simorgh in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-13-2010, 12:01 AM
  5. Thread.sleep() and join() question
    By sandeepsai39 in forum New To Java
    Replies: 5
    Last Post: 04-20-2009, 06:51 AM

Posting Permissions

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