Results 1 to 5 of 5
  1. #1
    willemien is offline Member
    Join Date
    Apr 2010
    Location
    london UK
    Posts
    53
    Rep Power
    0

    Question Threading Problem

    I have a program that has some thread's and now have problems

    One thread (the main thread) keeps adding strings to a TreeMap. (at a quite high rate)
    (with map.put(string1,string2);

    Another thread (started by pressing a button in a Gui) wants to copy that Tree map to an array.

    (with array = map.values().toArray(new String[0]);)

    and this never seems to happen, I guess because the main thread keeps adding strings.
    map should stay a treeMap because i want to have the array sorted. (and as far as I understand hashmaps don't do that)

    what is an easy and safe way to temporarely interrupt the main thread ?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: Threading Problem

    Look at using synchronized methods to do the add and the copy.

    http://docs.oracle.com/javase/tutori.../syncmeth.html
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    willemien is offline Member
    Join Date
    Apr 2010
    Location
    london UK
    Posts
    53
    Rep Power
    0

    Default Re: Threading Problem

    tried that

    synchronised the add and the getArray methods in the class that contains the map but it doesn't seem to work :(

    I even tried synchronising the map with

    private final Map<String, String> map = Collections.synchronizedSortedMap(new TreeMap<String,String>());
    but it still It just won't work

    Curiuosly i use the routine that makes an array from the map also in the main thread and in that thread it works fine.

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

    Default Re: Threading Problem

    It's hard to know what's not working without seeing your code, preferably a small compilable runnable program that shows your problem for us, an SSCCE.

  5. #5
    willemien is offline Member
    Join Date
    Apr 2010
    Location
    london UK
    Posts
    53
    Rep Power
    0

    Default Re: Threading Problem

    I t looks like i was not really paying attention to my program, did some refactoring and now it works. (think the problem was a testroutine that stopped the thread and because other threads kept running i didn't notice it

    made the methods synchronized (and converted them from class to instance methods) and now all seems to work fine :)

    Thanks for your help :)

Similar Threads

  1. Problem threading?
    By vcardell in forum New To Java
    Replies: 1
    Last Post: 05-01-2012, 09:00 AM
  2. Threading problem
    By Andyj in forum Threads and Synchronization
    Replies: 3
    Last Post: 12-05-2010, 10:27 PM
  3. Problem in Multi threading.
    By Chetans in forum Advanced Java
    Replies: 3
    Last Post: 03-23-2010, 05:42 PM
  4. Getting problem in threading in JAVA
    By Chetans in forum Threads and Synchronization
    Replies: 3
    Last Post: 03-19-2010, 08:49 AM
  5. Problem in threading
    By saurabh in forum Threads and Synchronization
    Replies: 6
    Last Post: 12-01-2008, 09:16 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
  •