0

I'm new on Multithreading and Synchronization in Java Android. As I known, Synchronization is usually use in Multithreading to deal with 'shared object' among these Multithreading. Also, Multithreading mean that many threads run parallel at the time.

My problem is, for example:

  • I have 2 object: cookie and monter.

  • Monter need eat cookie, then consume it in random speed time (not exceed 5s).

  • So, I have a small game: In 2 minutes. Two monter compete on eat cookie (Once eat 1 cookie, consume it). The contest's winner is determined as the monster that has eaten the most cookies in two minutes.

More detail. Cookie is likely 'shared object' and 2 monter is Multithreading

So, How to i make the game or simply how to i do maintain Threads (Loop: do and delay - eat and consume) in Java.

chibao
  • 23
  • 6
  • Have you tried to research any of the multithreading articles on the web? – bichito Feb 26 '17 at 16:26
  • Yes, I have read a lot about it. Almost solution is use thread sleep, but it not good. So im looking for a new and best solution. – chibao Feb 26 '17 at 16:30
  • I don't really see why Thread.sleep() wouldn't be a good solution for what you describe. Start by writing something before deciding it's not good enough. – JB Nizet Feb 26 '17 at 16:31
  • Then you need to change the language or the problem. – bichito Feb 26 '17 at 16:33
  • Thread.sleep() is not good because: http://stackoverflow.com/questions/8815895/why-is-thread-sleep-so-harmful – chibao Feb 26 '17 at 16:36
  • The first sentence of the accepted answer of the question you linked to says: *Thread.Sleep has its use: simulating lengthy operations while testing/debugging on an MTA thread. In .NET there's no other reason to use it.*. You are **not** writing a .net (or other server-side multiple request-handling program. You **are** simulating a lengthy operation (eating a cookie) to help you practice on threads. Real threads would do something useful to "eat a cookie". You're just simulating a lengthy operation. So Thread.sleep is exactly what you need. – JB Nizet Feb 26 '17 at 16:48
  • I suggest you read official docs and understand threading better. It would be amusing to find a Thread that waits without blocking or spinning loops, which would not yield control – bichito Feb 26 '17 at 16:48
  • JB Nized, thank for your interested. Please read my link carefully, then you will see "Thread.Sleep is pointless for timing". And if you link to other links below the accepted answer, you will see Thread.sleep is not exactly what i need – chibao Feb 26 '17 at 16:58
  • efekctive, thank for your interested. I will do what you said. I think i will use thread sleep until i see the best solution – chibao Feb 26 '17 at 17:00
  • 1
    @chibao You're quoting excerpts out of context without understanding what that means. The paragraph just talks about the fact the the precision of the system clock, and the thread scheduler, can't guarantee that a sleeping thead will wake up **exactly** after the number of milliseconds specified. That's irrelevant to your situation. You're here to ask questions to Java experts. I have 20 years of experience in Java programming. Maybe you should start considering my answers instead of neglecting them based on your misunderstandings of other threads. – JB Nizet Feb 26 '17 at 17:13
  • 1
    well JB Nizet, thank bro so much! Sorry for my bad. Now, Im go ahead with my problem (solve it). Kindly thank you again. – chibao Feb 26 '17 at 17:24

0 Answers0