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

    Default Another interesting task

    I need to place only one synchronized, so that Susan and John irons by turn, because there is only one iron.
    Java Code:
    public class Task {
        public static void main(String[] args) {
            Person susan = new Person("Susan");
            Person john = new Person("John");
        }
    
        public static class Person extends Thread { //Human
    
            public Person(String name) {
                super(name);
                start();
            }
    
            @Override
            public void run() {
                Iron iron = takinIron();
                Clothes clothes = takeClothes();
                ironing(iron, clothes);
                returnIron();
            }
    
            protected Iron takeIron() {
                System.out.println("Taking an Iron");
                return new Iron();
            }
    
            protected Iron returnIron() {
                System.out.println("Returning the Iron");
                return new Iron();
            }
    
            protected Clothes takeClothes() {
                return new Clothes("Shirt");
            }
    
            protected void ironing(Iron iron, Clothes clothes) {
                System.out.println(getName() + "'s ironing the " + clothes.name);
            }
        }
    
        public static class Iron {
        } // Iron
    
        public static class Clothes {// Clothes
            String name;
    
            public Clothes(String name) {
                this.name = name;
            }
        }
    }

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

    Default Re: Another interesting task

    Your statement makes no sense (although I believe I know what you want to do). However, you create a new iron each time someone returns the old iron. Probably not what you want to do.

    Also, when two threads vie for the same object, you must have some way for each thread to be waiting for the iron to become available. While one person is ironing, the other is waiting. When the first person is done, it signals the iron is available and goes into a wait state or does some other task.

    Unfortunately (and IMHO), your supplied code isn't close to doing what is required. You need to read up on threads and I would also suggest you check out some of the new concurrency classes in JDK 1.7. A good starting reference is in the Oracle tutorials --> Lesson: Concurrency (The Java™ Tutorials > Essential Classes)

    Regards,
    Jim
    Last edited by jim829; 10-30-2013 at 03:06 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: Another interesting task

    Thank you very much Jim! Sure, you're right, I just can add join() method and they will ironing in turn and that will solve a problem.
    But there is a condition to put only one synchronized so that code works correctly. I tried many variants, but can't find the right one...

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,536
    Blog Entries
    7
    Rep Power
    20

    Default Re: Another interesting task

    Synchronization is only part of the solution; you have to be 'in' a synchronized block, so you own the 'monitor'; being the owner of a monitor you can wait() for a certain condition or you can notify() others, waiting on the same condition. The condition here is the iron being not used.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: Another interesting task

    Ok, let me back up a sec. My previous oratory was geared toward sharing an object between two threads. In this case, the object is the iron. On the other hand, you could have a method where the ironing is being done. To ensure that neither thread may enter that method at the same time is trivial. Oh, and a caveat here. I am certainly not an expert on concurrent processing so others more in the know may have a different take.

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

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

    Default Re: Another interesting task

    thanks a lot Jim, I've found the solution - I just need to synchronize method run by Person.class:
    Java Code:
    public void run() {
                synchronized (Person.class) {
                    Iron iron = takeIron();
                    Clothes clothes = takeClothes();
                    ironing(iron, clothes);
                    returnIron();
                }
            }

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Another interesting task

    there is only one iron.
    Then should there only be one call to new Iron() and that one instance should be the one shared.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Another interesting task

    Like as in Singleton pattern, yes?

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

    Default Re: Another interesting task

    Not really. A singleton pattern is simply a technique by which one gets the same object returned regardless of how many calls to create it.
    In your case, I (we) assumed that you wanted to allocate a single iron, and share it between two threads.

    Regards,
    Jim
    Last edited by jim829; 11-01-2013 at 03:33 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Another interesting task

    Like as in Singleton pattern
    No.
    A Singleton would not use a new statement to get the ONE instance of the class. The code would need to call a static method that took care of creating and returning the one instance.
    If you don't understand my response, don't ignore it, ask a question.

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

Similar Threads

  1. Interesting task with regular expressinos
    By Bill SS in forum New To Java
    Replies: 3
    Last Post: 02-16-2012, 05:47 AM
  2. This is Cool and interesting but i failed
    By Jhovarie in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-07-2011, 04:00 AM
  3. interesting programming puzzle
    By karq in forum New To Java
    Replies: 4
    Last Post: 08-09-2010, 03:53 AM
  4. Interesting CS problem. Need help.
    By xtrmi in forum New To Java
    Replies: 1
    Last Post: 05-02-2009, 03:51 AM
  5. Interesting Experiment
    By uncommon in forum Advanced Java
    Replies: 13
    Last Post: 12-20-2008, 11:30 PM

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
  •