This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Allowing multiple threads into a monitor?  RSS feed

 
Tim Crouch
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have 3 resources that I am controlling access to, is there a way to allow 3 threads access to the resources using a monitor? Or are monitors strictly mutual exclusion?
 
Paul Clapham
Sheriff
Posts: 22487
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, a Semaphore (from the java.util.concurrent package) sounds like just the thing you're looking for.
 
Tim Crouch
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! This is actually for a homework assignment where the first part required the use of a counting semaphore to solve the same problem, and I got that working like a charm. But now the next part is to solve the same problem with a monitor, and once I add "synchronized" to the method that gives out the protected resources, it only allows in one thread at a time no matter what I do. Several people in my class have complained about this same problem, but I just wanted to try to exhaust every avenue before I slap another semaphore on it and call it a day lol. I have been researching and looking through links for hours trying to find an answer, but they all point to semaphores as being the answer for that problem.

I guess I was just trying to see if there was any way at all to do this with a monitor before conceding that it can't be done that way and going with the way I know works but isn't exactly what was asked for.

Thank you again
 
Paul Clapham
Sheriff
Posts: 22487
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you're supposed to write your own version of Semaphore using a monitor?
 
Tim Crouch
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is what we did in the first part, but the professor did seem to kind of cryptically agree that part 1 and 2 were a lot alike. He allows us to correct mistakes, so I guess I will just do that and see if he complains about it lol.

Thank you for the help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!