Re: using a boolean to control a thread
From: Ryan Stewart (zzanNOtozz_at_gSPAMo.com)
Date: 03/17/04
- Next message: Michael Scovetta: "Re: Post using Query String"
- Previous message: Ryan Stewart: "Re: Expression Language (EL) vs. Scriptlets in JSP"
- In reply to: Jeff: "using a boolean to control a thread"
- Next in thread: Quatto: "Re: using a boolean to control a thread"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Michael Scovetta: "Re: Post using Query String"
- Previous message: Ryan Stewart: "Re: Expression Language (EL) vs. Scriptlets in JSP"
- In reply to: Jeff: "using a boolean to control a thread"
- Next in thread: Quatto: "Re: using a boolean to control a thread"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|