Forums Register Login

is this the way to use join, sleep and Time unit sleep methods ?

+Pie Number of slices to send: Send
I dunno how to ask. I've been trying to implement thread concepts in real world scenario.
If you think you have good task much better than this then please post here. I'm also going to do Producer consumer task once I finish this.
  • One University Staff which has three teachers
    • Teacher One
    • Teacher Two
    • Teacher Three
  • 60 papers to check, It takes 2 seconds for each process( I know they are not robots but just assumed ).
    There are 3 process, these are grab paper from bunch of paper, check paper and put them in checked paper's bunch.
  • 3 Processes
    • Process One: grabPaper, only one teacher can access it at a time and
      rest will wait for this teacher to grab paper from bunch of papers which takes 2 seconds for each teacher
    • Process Two: checkPaper, Each teacher have different papers so they can check papers
      simultaneously once they get paper. Checking of one paper takes 2 seconds for each process
    • Process Three: putPaper, only one teacher can put it's checked paper in bunch of checked
      papers at a time and rest teachers will wait for this teacher to finish its job of keeping paper in that bunch
      which again takes 2 seconds for each teacher
  • All three teachers will take a rest of 10 seconds after their total checked papers
    becomes multiple of 10 i.e. rest at 10th, 20th, 30th up to..60th paper
  • Teacher Two will join working after 8 seconds Teacher One Starts
  • Teacher Three will join working after 5 seconds Teacher Two Starts
  • Yes it works successfully just want to know if there are any mistakes I made.
    Please correct me If I went wrong using these methods.
    +Pie Number of slices to send: Send
    +Pie Number of slices to send: Send
    +Pie Number of slices to send: Send
    Oh my god, I just need to learn how to post , edited so many times. doesn't it look weird?. Anyway need guidance for above code please.
    +Pie Number of slices to send: Send
     

    Ganish Patil wrote:Oh my god, I just need to learn how to post , edited so many times. doesn't it look weird?. Anyway need guidance for above code please.


    Well, just at a cursory glance, I'd say that you're looking at this much too mechanically, rather than looking at the objects involved.

    Unless I'm wrong, you have 3 Teachers - although there could just as easily be 47 - a bunch of Papers, but only TWO Piles (which sound suspiciously like Stack<Paper>s to me): 'unchecked' and 'checked'; so the only things that need to be synchronised are the push() and pop() methods for your Piles.

    Everything else - including timing - is irrelevant for anything other than "pausing" Teachers and Piles, and determining elapsed time.

    HIH

    Winston
    +Pie Number of slices to send: Send
     

    Winston Gutkowski wrote:Unless I'm wrong, you have 3 Teachers

    yes there are 3 teachers. What does Piles means ? I didn't get that.

    Winston Gutkowski wrote:(which sound suspiciously like Stack<Paper>s to me): 'unchecked' and 'checked'; so the only things that need to be synchronised are the push() and pop() methods for your Piles.

    Ohh ! yes yes I can use two Stack<Paper> one for checked and other for unchecked papers. There is no need of timing but just to see total work completion time taken when three teachers work simultaneously and when only one or two works (By not starting third teacher thread) and also how it pauses to know all this I used there.
    +Pie Number of slices to send: Send
    I got the start(), sleep() methods. For join() method as I have three threads in above example.
    So when I write teacherOne.join(5000); means teacherOne says to the following starting thread i.e. teacherTwo, wait for 5000 seconds when I start and then join me in sharing the resource. Correct me If I'm wrong about join() method.

    But I didn't get the difference between join() and wait();. What so far I understood is, when a thread which holds the lock or we can say current thread which owns the monitor can invoke this method. Suppose in the above example. I want to free only teacherOne for 5 seconds once 30 papers are checked. so teacherOne needs to invoke teacherOne.wait(5000); method like below

    I think compulsory we have to write wait code in synchronized block If I'm not wrong.
    I also need to write another condition to know 5 seconds have elapsed and the teacherTwo or teacherThree i.e. the current thread which owns monitor after 5 seconds
    must invoke notify() or notifyAll() method to resume teacherOne thread to start using resources again. Is this wait() method all about ? or something else.
    1
    +Pie Number of slices to send: Send
     

    Ganish Patil wrote:But I didn't get the difference between join() and wait()...


    Well, the docs for join() say that it's based on wait(), so it might be worth reading those.

    Suppose in the above example. I want to free only teacherOne for 5 seconds once 30 papers are checked...


    You still seem to be hung up on the mechanics of all this, instead of concentrating on the problem, and now you're trying all sorts of new things before you have ANY of them working.

    Based on what you described above, this is what I'd start out with:but there are probably many other ways to do it.

    See if you can work out what I'm trying to do, and come back if you have any questions.

    Winston
    +Pie Number of slices to send: Send
     

    Winston Gutkowski wrote:You still seem to be hung up on the mechanics of all this, instead of concentrating on the problem

    hmm I think so,

    See if you can work out what I'm trying to do, and come back if you have any questions.

    yes I'll make changes in the same above code, will use that Pile class. Thank you so much for your help. I'll post once I make changes in it.
    +Pie Number of slices to send: Send
     

    Ganish Patil wrote:yes I'll make changes in the same above code, will use that Pile class.


    OK, but make sure you understand why you're using it first.

    Winston

    PS: Made a minor change to avoid "double" synchronization.
    +Pie Number of slices to send: Send
    I beg your pardon, I know after long time I'm replying but I need to understand this concept. I hope this time I'm on right path. Here is my new code please correct me if I'm wrong.
    Pile CodeTeacherPaperCheckDemo
    You didn't tell me he was so big. Unlike this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 906 times.
    Similar Threads
    Thread,Join problem
    Confused with join concept
    Java Beginners FAQ
    OCMJEA FAQ
    Priority Semaphore/Lock
    Threads and Synchronization examples
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 29, 2024 09:23:23.