Concurrent Access does not modify an attribute

Good day,

I'm working on a multi-process environment, whose main class could be summed up as this:

public Class ServerCluster{

// In reality, this attribute belongs to a superior class and is protected

private SolverCluster solverCluster;

//...

publicvoid installSolver (SolverCluster newSolverCluster){

// Tests here to check the newSolverCluster isn't null

//...

sychronized (this){

System.out.println("A new SolverCluster is given");

this.SolverCluster = newSolverCluster;

}

}

publicvoid mainProcess(){

//...

while (true){

synchronized (this){

if (this.solverCluster !=null){

System.out.println("the solverCluster isn't null");

//That's where we want to go

//...

}

//...

}

thread.sleep(1000);

}

}

}

Basically, a process started from another class calls the method mainProcess and then stays inside.

Then, another Java process connects to the serverCluster (by RMI) and calls the method installSolver.

The problem is that the message "A solverCluster is given" is displayed, but when the process leaves the method, the attribute becomes null again and therefore the message "the solverCluster isn't null" in the mainProcess method is never displayed.

(I'm using NetBeans 4.1.).

Thank you

[2495 byte] By [Hanoya] at [2007-10-3 3:41:44]
# 1
It seems that you must use wait not sleep. wait is releasing the lock. in your code the lock is released between thread.sleep and synchronized (this). So i don't understand your System.out behaviour.maybe the parameter to installSolver is null?
Dicioa at 2007-7-14 21:37:35 > top of Java-index,Core,Core APIs...
# 2

> Basically, a process started from another class calls

> the method mainProcess and then stays inside.

Okay it loops checking then sleeping. Fine.

> Then, another Java process connects to the

> serverCluster (by RMI) and calls the method

> installSolver.

Presumably this is the same serverCluster object

> The problem is that the message "A solverCluster is

> given" is displayed, but when the process leaves the

> method, the attribute becomes null again and

> therefore the message "the solverCluster isn't null"

> in the mainProcess method is never displayed.

> (I'm using NetBeans 4.1.).

My best guess would be that they are different serverCluster objects.

Does the real code use instances or statics? If statics you'd have to watch for classes being loaded in different classloaders and therefore having their own static values.

davidholmesa at 2007-7-14 21:37:35 > top of Java-index,Core,Core APIs...