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 2 of 2

Thread: sync two diffrent kind of threads?

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default sync two diffrent kind of threads?

    hello, hopefully i will be able to explain this clearly..

    i have an static object named:
    public static Document dataFileXml;
    and, i have two kind of threads running:
    final class HttpRequest implements Runnable // (could be none, could be many)
    public class ReminderSender implements Runnable // (only one thread running all the time)

    - Both of them have functions that read and write to "dataFileXml".
    - i am familiar with sync,wait,notifyall method for shared memory between multiple threads of the same kind - but does this work with two kinds of threads?
    - anyway - does anyone have a good suggestion how to make sure only one thread will have access to dataFileXml and the rest will have to wait for their turn?

    Thanks in advanced!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: sync two diffrent kind of threads?

    Flank any changes to your dataFileXml object using synchronized

    synchronized (dataFileXml){
         ///make changes to dataFileXml
    }

    This will cause a thread to lock the object and force other threads to wait for it to unlock before any changes can be made. A thread is a thread, so whether or not the runnable interface is implemented between objects, they are all just threads.

Similar Threads

  1. [SOLVED] Questions About Threads
    By neo_2010 in forum Threads
    Replies: 4
    Last Post: March 15th, 2010, 09:04 AM
  2. threads in gui
    By urosz in forum AWT / Java Swing
    Replies: 1
    Last Post: November 3rd, 2009, 05:20 PM
  3. Which collection is best to do mathematical operation on it?
    By Sterzerkmode in forum Java Theory & Questions
    Replies: 1
    Last Post: May 7th, 2009, 04:48 AM
  4. [SOLVED] Fixing of bug for a small program
    By Koren3 in forum Threads
    Replies: 3
    Last Post: April 21st, 2009, 06:28 AM

Tags for this Thread