Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Is this a correct use of synchronization?

  1. #1
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    568
    Thanks
    0
    Thanked 104 Times in 89 Posts

    Default Is this a correct use of synchronization?

    Hi.

    I have 2 threads in my application.
    One of them is drawing the GUI at 30 frames per second.
    The other frame is doing calculations.
    The GUI thread should show the results of the calculating thread.

    To keep the GUI thread up to date I thought about building a queue in which the calculation thread is putting commands which the GUI thread has to execute.
    For this queue I have build a class which looks like this:
    public class CommunicationPipeline {
     
    	private static final Object lock = new Object();
     
    	private static LinkedList<Command> commands = new LinkedList<>();
     
    	private CommunicationPipeline(){}
     
    	public static void add(Command command) {
    		synchronized(lock) {
    			commands.add(command);
    		}
    	}
     
    	public static LinkedList<Command> get() {
    		synchronized(lock) {
    			LinkedList<Command> outgoingCommands = commands;
    			commands = new LinkedList<>();
    			return outgoingCommands;
    		}
    	}
     
    }

    And this is an example how these threads might communicate:
    public class CalculationThread extends Thread {
     
    	private boolean is_running;
     
    	public void run() {
    		while (is_running) {
    			// do calculations
    			CommunicationPipeline.add(new SomeCommand(...));
    		}
    	}
     
    }
    public class GUIThread extends Thread {
     
    	private boolean is_running;
     
    	public void run() {
    		while (is_running) {
    			LinkedList<Command> commands = CommunicationPipeline.get();
    			for (Command cmd : commands) {
    				cmd.execute();
    			}
    			// render everything with set frame rate
    		}
    	}
     
    }

    My question is, do I do this correctly? Is this a safe way to have 2 threads communicate with each other?

    Thanks in advance.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Is this a correct use of synchronization?

    It looks properly synchronized to me, though I'm not sure if this is the approach I would take. I'll have to dig through the Java API and think about it before I can suggest a better alternative.

  3. #3
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Is this a correct use of synchronization?

    Hello.
    In my view the synchronization seems to be done very well.

    Syed.

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    568
    Thanks
    0
    Thanked 104 Times in 89 Posts

    Default Re: Is this a correct use of synchronization?

    Thanks for the answeres; I appreciate the great help.

Similar Threads

  1. synchronization
    By bassie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 12th, 2013, 07:29 AM
  2. problem in synchronization
    By codenoob in forum Threads
    Replies: 3
    Last Post: December 8th, 2011, 07:29 AM
  3. Synchronization on object
    By god1gracious in forum Java Theory & Questions
    Replies: 1
    Last Post: September 23rd, 2011, 01:13 PM
  4. synchronization problems
    By stroodlepup in forum Threads
    Replies: 2
    Last Post: February 28th, 2011, 08:26 AM
  5. synchronization
    By bbr201 in forum Java Theory & Questions
    Replies: 6
    Last Post: August 29th, 2010, 09:32 AM