Re: using a boolean to control a thread

From: Ryan Stewart (zzanNOtozz_at_gSPAMo.com)
Date: 03/17/04


Date: Tue, 16 Mar 2004 17:50:41 -0600


"Jeff" <jeffdrew@bellatlantic.net> wrote in message
news:4WJ5c.15119$1g2.294@nwrdny02.gnilink.net...
> I'd like to control a thread's existance using some variable I pass it.
> However, I can't figure out how to change the value of a boolean object.
> The following code runs. The problem is in control.valueOf( false ) -
that
> does not set control to false. I've included the programs to provide a
> better understanding of my intent. These Classes will be in separate
files
> normally. Right now I use global boolean variables but it's ugly.
>
> What is the correct way to change the Boolean value? Is this a best
> practice way to control my threads?
>
> Thanks
>
> Jeff
>
> public class MasterThread {
>
> MasterThread() {
> Boolean control = new Boolean( true );
>
> SlaveThread slave = new SlaveThread( control );
> Thread t = new Thread( slave );
> t.start();
>
> try { Thread.currentThread().sleep(1000); } catch (Exception e) {}
>
> control.valueOf( false );
> System.out.println("Master set control to " +
> control.booleanValue() );
>
> try { Thread.currentThread().sleep(1000); } catch (Exception e) {}
> }
> /**
> * @param args the command line arguments
> */
> public static void main(String[] args) {
> MasterThread mt = new MasterThread();
> }
> }
>
> class SlaveThread implements Runnable {
> Boolean existance;
>
> SlaveThread( Boolean existanceArg ) {
> existance = existanceArg;
> }
> public void run() {
>
> while ( existance.booleanValue() ) {
> System.out.println("running");
> try { Thread.currentThread().sleep(500); } catch (Exception e)
> {}
> }
> }
> }
>

Why use Boolean? Why not boolean? And why pass it to the constructor? Will
you ever create a SlaveThread that you don't want to run? I doubt it, so
just initialize it to true within that class. As for your logic, I think I
begin to see why you used Boolean. You want to have all threads referencing
one object and use that to switch all threads off at once? As Cristophe
mentioned, that won't work. First of all, if you read the spec for Boolean,
you'll see that the valueOf method returns a Boolean object. It doesn't
change the state of an object that you call it on. In fact, as Cristophe
also pointed out, you cannot change the state of a Boolean object. All of
the wrapper classes are immutable. So you see, even if you do change the
MasterThread's Boolean to false, you'd actually be causing the variable to
reference a new obect, and the SlaveThreads will not care about it one
little bit. Finally, and a minor point, usage of the Boolean(boolean value)
constructor is strongly discouraged. Either use the valueOf() method to
obtain an instance or use Boolean.TRUE and Boolean.FALSE.

A possible solution for you: if all you want is to have all threads stop
executing when your main thread ends, have a look at the setDaemon method of
Thread. Other solutions, as Cristophe mentioned, are to create your own
boolean wrapper, like Switch, that allows you to set its state, or to add a
setter to SlaveThread to tell it when to stop. The latter method requires
you to maintain a reference to every thread you create though.



Relevant Pages

  • Re: VB 2005 Express Third Party Control
    ... Details of the control itself and links to the dll's required are on; ... ListBarGroupView view, Int32 scrollOffset, Boolean controlEnabled, ... ListBarDrawStyle style, ListBarGroupView view, Boolean enabled, Int32 ...
    (microsoft.public.dotnet.languages.vb.controls)
  • Re: Optional Arguments in Sub Procedure
    ... Doug Steele, Microsoft Access MVP ... Sub setCntrlProp(bx As Control, btn As Control, btnVis As Boolean, bxVis ... Optional ctlVis2 As Boolean, Optional ctl3 As ...
    (microsoft.public.access.gettingstarted)
  • Re: Optional Arguments in Sub Procedure
    ... Doug Steele, Microsoft Access MVP ... Sub setCntrlProp(bx As Control, btn As Control, btnVis As Boolean, bxVis ... Optional ctlVis2 As Boolean, Optional ctl3 As ...
    (microsoft.public.access.gettingstarted)
  • RE: Database design - enforcing referential integrity on control table
    ... My suggestion would be to include a boolean field in your state and country ... tables to control whether they are selectable or not. ...
    (microsoft.public.inetserver.asp.general)
  • Re: Optional Arguments in Sub Procedure
    ... Doug Steele, Microsoft Access MVP ... Sub setCntrlProp(bx As Control, btn As Control, btnVis As Boolean, bxVis ... Optional ctlVis2 As Boolean, Optional ctl3 As ...
    (microsoft.public.access.gettingstarted)