someone who knows java PLEASE GOD HELP ME

Discussion in 'OT Technology' started by RacerJ, Feb 8, 2008.

  1. RacerJ

    RacerJ New Member

    Joined:
    May 8, 2005
    Messages:
    8,034
    Likes Received:
    1
    Location:
    austin
    K i'm taking a class on Concurrent and Distributed systems using java. Right now we're programming threads and using Semaphores and stuff to make programs run concurrently.

    I honestly have no clue how to effectively use Semaphores. I know you call P() before a thread performs something in the critical section, and then you call V() to release. I cant really apply any of this to anything without having some actual java examples of this. Can anyone extrapolate the use of this using multiple java classes? maybe give me some basic examples that i can discuss/build off of? I feel like i'm starting to grasp the very very beginning concepts of this, but since i dont have any real examples with good explanations that i cant make the jump to actually understanding this
     
  2. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    I don't know if Java has any built-in implementations of semaphores, but the basic concept is this:

    You have functions running in separate threads and they sometimes step on each other. Whichever function needs to run first should set a global boolean (i.e. "bool function_x_notready;") to True when it starts running, and set it back to False when it finishes running. The other, dependent functions should check that boolean (e.g. "if (function_x_notready) {return false;}") at the very beginning of their code.

    Presumably there's a way to tell the thread to sleep for n CPU cycles instead of just bailing out using "return false" like I did, but I don't know the syntax for doing that in Java and I don't feel like looking it up at 5:30am. Also, if you have one set of functions that needs to run before another set of functions, you'll want to use an int instead of a bool as your semaphore, so you can increment and decrement the semaphore to reflect how many non-interfering functions are running at any given time. In that case, the dependent functions would check to see if the semaphore == 0 instead of == false, before trying to run.

    - - -

    One example where I use semaphores is in a program I wrote that allows the user to change the priority and CPU allocation of processes running on their machine. The code that changes the settings on processes to match the user's specifications requires that those specs must not be in the middle of getting changed when it does it's comparison; as such, when any command is issued that changes a process' settings, the first thing I do is trip a semaphore that tells the process checker to bail out and wait for the timer to reset before running again. That way the process checker never ever ever tries to check settings until those settings are stable.
     
  3. b0b

    b0b OT Supporter

    Joined:
    Feb 17, 2004
    Messages:
    113
    Likes Received:
    0
    Location:
    Orlando, FL
    i did an assignment in java for a class last semester. it was a producer/consumer problem using semaphores. shoot me a pm, i can send you my java files or something.
     

Share This Page