Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31721 > unrolled thread

Main Loop porblm

Started byMichael <progressman757@gmail.com>
First post2015-05-18 06:50 -0700
Last post2015-05-18 11:15 -0400
Articles 4 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Main Loop porblm Michael <progressman757@gmail.com> - 2015-05-18 06:50 -0700
    Re: Main Loop porblm Michael <progressman757@gmail.com> - 2015-05-18 07:05 -0700
    Re: Main Loop porblm Silvio <silvio@internet.com> - 2015-05-18 17:01 +0200
    Re: Main Loop porblm Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-05-18 11:15 -0400

#31721 — Main Loop porblm

FromMichael <progressman757@gmail.com>
Date2015-05-18 06:50 -0700
SubjectMain Loop porblm
Message-ID<c666ab49-a6d4-4ad0-b4a5-4ab7da6d5c97@googlegroups.com>
Simple window application with JSlider. Thereis main loop "while(true)" and "if(show==true)" statement, inside it we done "show=true" and "System.out.println("+")".

At the Slider Listener we change variable show to true.


When program starts we can see one plus. Its Ok. When mooving slider variable "show" changed to true like we want, but IF statement in main loop does not work. What is the solution? Sorry for the simple question :)


	    while(true)
	    {
	    	if(show == true)
		    {
			    show = false;
			    System.out.println("+");
		    }
	    }

class SliderListener implements ChangeListener {
    public void stateChanged(ChangeEvent e) {
        JSlider source = (JSlider)e.getSource();
        if (!source.getValueIsAdjusting()) {
            int fps = (int)source.getValue();

            Loop.show = true;

            System.out.println((int)source.getValue());

        }    
    }
}

[toc] | [next] | [standalone]


#31722

FromMichael <progressman757@gmail.com>
Date2015-05-18 07:05 -0700
Message-ID<cdec7532-20fc-4056-9d6d-2c08100c09bc@googlegroups.com>
In reply to#31721
Solution is: replace public static boolean show = true;
with public static volatile boolean show = true;

[toc] | [prev] | [next] | [standalone]


#31723

FromSilvio <silvio@internet.com>
Date2015-05-18 17:01 +0200
Message-ID<5559feaf$0$2877$e4fe514c@news2.news.xs4all.nl>
In reply to#31721
On 05/18/2015 03:50 PM, Michael wrote:
> Simple window application with JSlider. Thereis main loop "while(true)" and "if(show==true)" statement, inside it we done "show=true" and "System.out.println("+")".
>
> At the Slider Listener we change variable show to true.
>
>
> When program starts we can see one plus. Its Ok. When mooving slider variable "show" changed to true like we want, but IF statement in main loop does not work. What is the solution? Sorry for the simple question :)
>
>
> 	    while(true)
> 	    {
> 	    	if(show == true)
> 		    {
> 			    show = false;
> 			    System.out.println("+");
> 		    }
> 	    }
>
> class SliderListener implements ChangeListener {
>      public void stateChanged(ChangeEvent e) {
>          JSlider source = (JSlider)e.getSource();
>          if (!source.getValueIsAdjusting()) {
>              int fps = (int)source.getValue();
>
>              Loop.show = true;
>
>              System.out.println((int)source.getValue());
>
>          }
>      }
> }
>

 From the limited amount of information you provide it is impossible for 
us to determine what you are trying to achieve. But whatever you want to 
do you have given enough details for us to conclude you are doing it wrong.

Please provide more information and we might be able to help.

[toc] | [prev] | [next] | [standalone]


#31724

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2015-05-18 11:15 -0400
Message-ID<mjcvk3$4gl$1@dont-email.me>
In reply to#31721
On 5/18/2015 9:50 AM, Michael wrote:
> Simple window application with JSlider. [...]

     From the follow-up, it seems you have solved your problem.
However, there's still a small matter of style:

> 	    	if(show == true)

Do you see anything odd about this test?  The comparison is
between a boolean variable (whose value is `true' or `false')
and the boolean constant `true', and the result of comparing
is either `true' or `false'.  In fact, the result of the
comparison is whatever value `show' itself has: The comparison
has made no "progress."  You began with a value that is either
`true' or `false', and you arrive at an expression whose value
is either `true' or `false'.  If you want to check the value of
the expression, shouldn't you write

	if ((show == true) == true)

?  Well, no: You're still stuck with a `true' or `false' result,
and you still need to test it.  So, maybe

	if (((show == true) == true) == true)

would be an improvement?  Um, no, and it's beginning to look like
adding more and more `== true' comparisons might not be the right
way to go.  Hmmm: What to do?

	if (show)

... might be an attractive option, don't you think?

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web