Results 1 to 10 of 10
  1. #1
    d3m3tri0 is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default Help me please with synchronization

    Hello everyone! I have a task to remove excess (unnecessary) synchronized inside methods, because synchronized slows down the code. Help me to do this please.

    Java Code:
    public class Task {
        char[] value;
        int count;
     
        public Task append(CharSequence s) {
            synchronized (Task.class) {
                if (s == null) {
                    synchronized (this) {
                        s = "null";
                    }
                }
     
                if (s instanceof String) {
                    synchronized (this) {
                        return this.append((String) s);
                    }
                }
     
                if (s instanceof Task) {
                    synchronized (this) {
                        return this.appendThis((Task) s);
                    }
                }
            }
            return this.append(s);
        }
     
        public synchronized Task appendThis(Task s) {
            //do something here....
            return this;
        }
     
        private static final java.io.ObjectStreamField[] serialPersistentFields =
                {
                        new java.io.ObjectStreamField("value", char[].class),
                        new java.io.ObjectStreamField("count", Integer.TYPE),
                        new java.io.ObjectStreamField("shared", Boolean.TYPE),
                };
     
        private synchronized void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
            java.io.ObjectOutputStream.PutField fields = s.putFields();
            synchronized (fields) {
                fields.put("value", value);
                fields.put("count", count);
                fields.put("shared", false);
            }
            synchronized (s) {
                s.writeFields();
            }
        }
     
        private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {
            synchronized (new java.io.IOException()) {
                java.io.ObjectInputStream.GetField fields = s.readFields();
                value = (char[]) fields.get("value", null);
                count = fields.get("count", 0);
            }
        }
    }
    Last edited by d3m3tri0; 10-30-2013 at 01:14 PM.

  2. #2
    foulkelore is offline Member
    Join Date
    Dec 2012
    Location
    Des Moines, IA
    Posts
    35
    Rep Power
    0

    Default Re: Help me please with synchronization

    Do you have a specific question? I don't think anyone here is going to do it for you.

  3. #3
    d3m3tri0 is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default Re: Help me please with synchronization

    I think it's not a problem to tell what synchronized is unnecessary here to a person who is good in concurrency. Don't do anything else.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default Re: Help me please with synchronization

    because synchronized slows down the code
    Prove it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    d3m3tri0 is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default Re: Help me please with synchronization


  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default Re: Help me please with synchronization

    I am not going to read an article that is over 10 years old. Prove it with something that is of this time. And by the way: linking to some article on the net is not proof. Create code which proves that by introducing synchronization it becomes slower.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: Help me please with synchronization

    I haven't tested it but it seems to me that you would be better served by overloading the append method to accept either a Task or a String object. Then you could avoid the instance tests.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    d3m3tri0 is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default Re: Help me please with synchronization

    sure you're right, many methods in java 6 and 7 are synchronized, that hasn't considerable influence on program performance. I've also read that synchronized block in Java may slow down only single-threaded application. I'm agree that the best way to demonstrate this is to write a code and note its execution time. But I'm new to Java and I can't do it now, I just need to make this task for my classes. This task condition was "to remove unnecessary synchronized inside methods because synchronized slows down the code." May be it was wrong formulated, I need to ask someone competent.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,360
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help me please with synchronization

    You don't synchronize something just for the fun of it; you synchronize with a purpose; inside a synchronized block your code can wait() for something or notify() something. They are the building blocks for exclusion. You also synchronize on something, an object; that's how Java solved its synchronization problem, i.e. every object has a 'monitor', needed by the synchronization mechanism behind the scenes.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    d3m3tri0 is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default Re: Help me please with synchronization

    okay I see, thank you Jos!

Similar Threads

  1. I need help in a synchronization threads
    By Alexis in forum Threads and Synchronization
    Replies: 0
    Last Post: 05-09-2013, 04:18 AM
  2. Need help with synchronization
    By boblettoj99 in forum Threads and Synchronization
    Replies: 1
    Last Post: 05-08-2012, 03:22 AM
  3. need help in locking or synchronization
    By samanyu in forum New To Java
    Replies: 24
    Last Post: 07-06-2011, 06:19 AM
  4. Thread synchronization
    By rajanis in forum Threads and Synchronization
    Replies: 3
    Last Post: 02-18-2011, 12:30 PM
  5. Thread synchronization
    By rajanis in forum Threads and Synchronization
    Replies: 0
    Last Post: 01-07-2011, 07:38 AM

Tags for this Thread

Posting Permissions

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