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

Thread: Synchronized is blocking

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Synchronized is blocking

    I'm just starting up with Java Mulithreading.
    Below is the repo I set up, and I am having trouble with this test I am doing playing around with Executor threadpools. I am trying to setup a managed system where I first add some lines to be printed, then add the work to the work pool, and then finally launch n number of threads to handle the work (or in this case, print out the strings I added to WorkManager instance fields). I watched this series of videos starting with this one: https://www.youtube.com/watch?v=YdlnEWC-7Wo, and I planned on playing with the concept of multithreading, but it doesn't seem to work and gets stuck at Thread1:27. I thought that by synchronizing access to the ListIterator return, that it would not block.

    https://github.com/hhbhagat/ThreadPoolTest

    Thanks.


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Synchronized is blocking

    I'm not too sure about your syntax
        public T getSomeWork() {
            synchronized (getwork) {
                return iterWorkList.next();  //This gives some work/work info to the calling method, and gives out whatever T is from the iterWorkList iterator.
            }
        }

    I'm thinking it should read something like
    public synchronized T getSomeWork() {
        return iterWorkList.next();
    }

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Synchronized is blocking

    I had thought that putting synchonized in the method header was a bad thing because it would then lock up the entire class from being accessed by another thread.

    EDIT: I made the method synchronized, but it still does not run properly, and stops after the 10 workers have started.
    Last edited by hhbhagat; May 13th, 2014 at 09:16 AM.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Synchronized is blocking

    Quote Originally Posted by hhbhagat View Post
    I had thought that putting synchonized in the method header was a bad thing because it would then lock up the entire class from being accessed by another thread.
    Why do you think that? Have you read through the tutorials? If not, start here: Lesson: Concurrency (The Java™ Tutorials > Essential Classes)

    Quote Originally Posted by hhbhagat View Post
    EDIT: I made the method synchronized, but it still does not run properly, and stops after the 10 workers have started.
    What does "not run properly" mean? Where is the code locking up? Use a debugger or even some print statements to figure that out.

    Read through the tutorials and post an MCVE instead of a link to your whole project, and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    May 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Synchronized is blocking

    Ok, So I checked it out, and I was getting ConcurrentModificationExceptions out the wazoo. I am guessing that since it was a different thread, those exceptions (that i didn't try-catch) didn't bubble up to the main output. Im going to try and see if I can understand more of what is going on from KevinWorkman's link.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Synchronized is blocking

    That Exception is caused when you attempt to modify a Collection (such as a List or a Map) while you're iterating over it. I assume that one thread is iterating over a collection while another thread is modifying it.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    May 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Synchronized is blocking

    Well, in my main, I first have it adding strings to the tasks to be completed, and then I have several threads added to a pool where they are left to their own devices getting work from the pool. So I am not adding AND getting at the same time directly, but getNextJob may be causing it to be so because of an internal index in the ListIterator interface. Am I correct? I tried to make it a volatile List, but I am still getting the exception.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Synchronized is blocking

    You don't have to be adding and getting at the same time to see the Exception, you just have to be modifying the collection while you're iterating over that collection But like I said, without an example SSCCE, it's going to be pretty hard to help you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. InputStreamReader blocking threads
    By coffeecups in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 5th, 2014, 03:25 PM
  2. Why isn't my synchronized function acting synchronized?
    By ineedahero in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 20th, 2013, 10:29 AM
  3. Synchronized block vs Synchronized method
    By tcstcs in forum Java Theory & Questions
    Replies: 1
    Last Post: April 20th, 2011, 07:51 AM
  4. Question about blocking queue
    By Kerr in forum Threads
    Replies: 2
    Last Post: April 8th, 2011, 04:59 AM
  5. blocking a network connection
    By BraveProgrammer in forum Java Networking
    Replies: 4
    Last Post: September 2nd, 2010, 12:08 PM