finishing a thead

C

Christian Schmidt

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
 
A

Adam Maass

Christian Schmidt said:
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;
}
}
 
K

Knute Johnson

Adam said:
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.
 
A

Adam Maass

Knute Johnson said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top