finishing a thead

Discussion in 'Java' started by Christian Schmidt, Aug 21, 2003.

  1. Hello,

    I'm using threads for the first time and got some trouble with finishing
    a thread.

    An example:

    In a constructor of class1 I create and start a new threat (class2).
    Whenn calling the shutdown func. of class1 I want to finish the threat
    (class2). The run method of class2 looks like:


    private volatile boolean doSomething = true:
    public void run {
    while (doSomething) {
    ...
    }
    System.out.writeln("End.");
    }

    And there is a stopIt method I call from class1 in the shutdown func:
    public stopIt()
    {
    ...
    this.doSomething = false;
    }

    But the "End." will never dumped...
    What is the right way... ??

    thanks in advance for help

    Chriss
    Christian Schmidt, Aug 21, 2003
    #1
    1. Advertising

  2. Christian Schmidt

    Adam Maass Guest

    "Christian Schmidt" <> wrote:
    >
    > I'm using threads for the first time and got some trouble with finishing
    > a thread.
    >
    > An example:
    >
    > In a constructor of class1 I create and start a new threat (class2).
    > Whenn calling the shutdown func. of class1 I want to finish the threat
    > (class2). The run method of class2 looks like:
    >
    >
    > private volatile boolean doSomething = true:
    > public void run {
    > while (doSomething) {
    > ...
    > }
    > System.out.writeln("End.");
    > }
    >
    > And there is a stopIt method I call from class1 in the shutdown func:
    > public stopIt()
    > {
    > ...
    > this.doSomething = false;
    > }
    >
    > But the "End." will never dumped...
    > What is the right way... ??
    >
    > thanks in advance for help
    >



    It might be because you've written System.out.writeln rather than
    System.out.println. Or maybe the code you call inside the loop doesn't ever
    return.


    BTW, the use of volatile on your variable doSomething (counterintuitively)
    isn't guaranteed to work. The only safe way to check the variable is to make
    both the read and the write (in your method stopIt) synchronized.




    class MyRunnable implements Runnable{
    private boolean doSomething = true;
    public void run {
    while (isDoSomething()) {
    // code called here needs to return
    }
    System.out.println("End.");
    }

    synchronized boolean isDoSomething(){
    return doSomething;
    }

    synchronized void stopIt(){
    doSomething = false;
    }
    }
    Adam Maass, Aug 21, 2003
    #2
    1. Advertising

  3. Adam Maass wrote:
    > BTW, the use of volatile on your variable doSomething (counterintuitively)
    > isn't guaranteed to work. The only safe way to check the variable is to make
    > both the read and the write (in your method stopIt) synchronized.


    Is there some bug in volatile? That's what the docs say it is supposed
    to do.

    --

    Knute Johnson
    email s/nospam/knute/
    Molon labe...
    Knute Johnson, Aug 22, 2003
    #3
  4. Christian Schmidt

    Adam Maass Guest

    "Knute Johnson" <> wrote:
    > Adam Maass wrote:
    > > BTW, the use of volatile on your variable doSomething

    (counterintuitively)
    > > isn't guaranteed to work. The only safe way to check the variable is to

    make
    > > both the read and the write (in your method stopIt) synchronized.

    >
    > Is there some bug in volatile? That's what the docs say it is supposed
    > to do.
    >


    The relevant chapter of the Java Language Specification is Chapter 17.

    In short, volatile requires that thread T writes back to main memory any
    changed value as soon as the write happens. But thread U may already have
    read (and cached) the variable from main memory; it may see stale values.
    (At least, that's my understanding of the problem.)

    One very good source for these issues is here:

    http://www.cs.umd.edu/~pugh/java/memoryModel/

    -- Adam Maass
    Adam Maass, Aug 25, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Erik Johnson
    Replies:
    1
    Views:
    406
    Donn Cave
    Aug 17, 2004
  2. jkn
    Replies:
    9
    Views:
    9,086
    robin
    Feb 27, 2006
  3. tshad

    File Download not finishing Sub

    tshad, Nov 7, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    380
    tshad
    Nov 8, 2006
  4. shapper
    Replies:
    0
    Views:
    344
    shapper
    Mar 4, 2008
  5. praveen praveen
    Replies:
    3
    Views:
    101
    Andrea Dallera
    Mar 18, 2010
Loading...

Share This Page