Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Variable not updated

Variable not updated

Scheduled Pinned Locked Moved Java
question
8 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kujtim Hyseni
    wrote on last edited by
    #1

    Hello, I have a code which checks some variable which has to be changed from false to true in other class, but, even if it changes it does not appears. More details, I have the code:

    ms.isDataReady = false;
    ...
    while(!ms.isDataReady);

    the loop while waits forever in my main method even if

    ms.isDataReady

    becomes true in

    ms

    's appropriate method. What I should do?

    L 1 Reply Last reply
    0
    • K Kujtim Hyseni

      Hello, I have a code which checks some variable which has to be changed from false to true in other class, but, even if it changes it does not appears. More details, I have the code:

      ms.isDataReady = false;
      ...
      while(!ms.isDataReady);

      the loop while waits forever in my main method even if

      ms.isDataReady

      becomes true in

      ms

      's appropriate method. What I should do?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      How can it change to true if the loop is running?

      Veni, vidi, abiit domum

      K 1 Reply Last reply
      0
      • L Lost User

        How can it change to true if the loop is running?

        Veni, vidi, abiit domum

        K Offline
        K Offline
        Kujtim Hyseni
        wrote on last edited by
        #3

        It changes somewhere else but it doesn't affects the change in while in other words it doesn't appears changed, unless I use a tricky solution so instead of

        while(!ms.isDataReady);

        I put

        while(!ms.isDataReady) Thread.sleep(1);

        L 1 Reply Last reply
        0
        • K Kujtim Hyseni

          It changes somewhere else but it doesn't affects the change in while in other words it doesn't appears changed, unless I use a tricky solution so instead of

          while(!ms.isDataReady);

          I put

          while(!ms.isDataReady) Thread.sleep(1);

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Your while statement is not the best way to wait for something to happen, as it will just lock the processor, spinning on that statement.

          Veni, vidi, abiit domum

          K 1 Reply Last reply
          0
          • L Lost User

            Your while statement is not the best way to wait for something to happen, as it will just lock the processor, spinning on that statement.

            Veni, vidi, abiit domum

            K Offline
            K Offline
            Kujtim Hyseni
            wrote on last edited by
            #5

            What do you suggest instead ... ?

            L 1 Reply Last reply
            0
            • K Kujtim Hyseni

              What do you suggest instead ... ?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Well that depends on what you are trying to do. What is the interaction between these two threads and what is the first one waiting for?

              Veni, vidi, abiit domum

              K 1 Reply Last reply
              0
              • L Lost User

                Well that depends on what you are trying to do. What is the interaction between these two threads and what is the first one waiting for?

                Veni, vidi, abiit domum

                K Offline
                K Offline
                Kujtim Hyseni
                wrote on last edited by
                #7

                Here are the details of the problem: I have a class MISPSerial (instance named ms) which deals for stream of bytes to arrive via serial port. In my class which contains the main method I wait for the variable (ms.isDataReady) in MISPSerial which tells that the stream is received completely. So my main method waits for the stream to be received and then continues with instructions that follow. What would you suggest?

                L 1 Reply Last reply
                0
                • K Kujtim Hyseni

                  Here are the details of the problem: I have a class MISPSerial (instance named ms) which deals for stream of bytes to arrive via serial port. In my class which contains the main method I wait for the variable (ms.isDataReady) in MISPSerial which tells that the stream is received completely. So my main method waits for the stream to be received and then continues with instructions that follow. What would you suggest?

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Run the class in the main thread; why do you need multiple threads when the main thread is always blocked on the other one?

                  Veni, vidi, abiit domum

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups