We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi all, is some days i'm doing experiments on running threads in parallel. Except the fact that probably this is not the best way to do this, since would be possible to point some external variable, i really don't understand why, if i try to access a variable inside a runnable implementation it works, but it don't if this implementation is trying to do the same:
class0 c0;
void setup() {
size(200, 200);
c0 = new class0();
new Thread(c0).start();
}
void draw() {
background(200);
text(c0.dataToAccess, 30, 30);
}
class class0 implements Runnable {
public int dataToAccess;
public int interations = 0;
class1 c1;
class0() {
c1 = new class1();
new Thread(c1).start();
}
//@Override
public void run() {
while (!c1.done) {
this.dataToAccess =c1.dataToAccess;
// println("iteration n" + interations++);
}
}
}
class class1 implements Runnable {
public int dataToAccess;
public boolean done = false;
//@Override
public void run() {
for (int i = 0; i<=100; i++)
{
dataToAccess++;
delay(500);
println("iteration n° " + i);
}
done = true;
}
}
Answers
Sorry, I'm not getting what exactly isn't working? :|
The fact any
class
is Runnable or not doesn't change in any way the scope of its members.BtW, I've made some small tweaks in your code just so I could study it better: B-)
But yet, you still need to explain further what's exactly wrong. :-\"
Well, actually yours is written in a more elegant way. Actually my problem is that the Class0 was unable to access the dataToAccess into Class1, till i've introduced on my old code the same delay you added into the Class0 run() at line 32. Can be a concurrency access problem? Would be possible as well to use a synchronized variable or that is just for methods? Also, you declared the Class1 like final, is that for efficiency like it would be for a variable? So many questions, i know :-)
Like this for example?
I confess the 1st thing I did prior to run your code for the 1st time was adding
delay(10);
. :PB/c I know that an "infinite" loop w/o doing nothing else other than checking a variable would "fry" the CPU core running that Thread! :-SS Therefore I've unintentionally skipped your "bug" completely. :-\"
In Java, each Thread got its own piece of RAM. And there's also the main RAM.
In order for a Thread to "see" the current value of some field which was just mutated by another Thread, that field needs to be updated to the main RAM 1st. L-)
My theory is that perhaps w/o any delay(), there wouldn't be any time gap for the transfer from main RAM to the always-busy Thread's RAM to occur. :-?
synchronized
is only for methods and{}
blocks.For non-
final
fields we can declare them asvolatile
w/ some small performance "sacrifice". $-)final
fields don't needvolatile
b/c they can't be reassigned after all. \m/Well, in some cases it might give more room to Java's VM to be more aggressive towards optimization, given it's guaranteed the value stored in the variable won't change ever! :)>-
A really gr8 series of articles about Java's concurrency: ~O)
http://Tutorials.Jenkov.com/java-concurrency/index.html