Hello everybody~ First, thanks for all of you who helped on my previous post! They all worked well!! Here is a problem I still can't solve. I want to have a set of variables (or can be a class) that is shared among my threads. The hierachy of my program is as follows: Main calls thread1, also thread2. Thread1 then calls thread1b, and thread1c. They all simultaneously needs to access and modifiy the set of variables I talked about earlier. For now, lets not worry about locking those variables, for those just have to be primitive and array type of little size. For now, I created a class and some methods for those variables, and I passed that object into every thread, thinking that Java will pass the memory address of the object to the threads. Then, all threads will be refering to the same object when they access or modify it. But it seems does not work ... How can I solve this? Thanks everyone!! Jack
What you're describing is the correct and usual way to handle this; the devil is in the details, of course. Can you show us the (perhaps simplified) actual code you've written? I suspect we'll find that you haven't implemented exactly what you've described.
Well, OK, that looks correct, from what I can see. So why do you say it's not working? Show me a method that's getting an unexpected result. Note that, although you've said we oughtn't worry about synchronization, ignoring synchronization might actually be the root of the problem. If you ignore synchronization, a specific memory location -- i.e., a member variable of a single object -- can appear to have different values as seen from different threads. It is only when a memory barrier is crossed -- for example, when a synchronized block is entered -- that all threads are guaranteed to agree on the values of a variable. Changes made from one thread aren't guaranteed to instantly propagate to all the others otherwise. While we're here, let me give you a little advice. The elements of a newly-created Java array are always initialized to null or 0 or 0.0 or false, depending on type; there's no need for you to do it explicitly. Similarly, member variables of type int are initialized to zero, booleans to false, etc. Therefore most of your constructor body is actually doing redundant work. Only the lines that set variables to non-zero values should be retained. [ March 09, 2004: Message edited by: Ernest Friedman-Hill ]
Well, let's see. You can test the theory that this is the problem by declaring all the members of NodeState to be "volatile." This is quite bad for performance, however, so it's not something you'll want to do in the long term. But if you change all the declarations to, i.e., public volatile boolean notDone; and the problem goes away, then you know that was it. If that really is the problem, then you'll need to look at synchronization more closely. Any "simple" solution I give you will be a non-optimal one. For example, you could deal with this by using synchronized get/set methods to access every member variable. While using the methods is a good idea, making everything synchronized isn't, necessarily. The right answer depends on your application.
Firstable, I really need to thank you for your immediate replies!! It really helps alot! First, to ask perhaps a really simple questions, the get/set methods, to make them synchronized, all I have to do is add the keyword "synchonized" at their heading and that'll ensure synchronization between different thread access/modify them? So what happens when a thread tries to access a block that is locked by other already? Does it just wait, till it can enter it? Then, 2nd questions is, notice I am trying to do TimeStamps, however, when I do System.currentTimeMillis(), I realize some values are 1xxxxxxxxxx while others could be 4xxxxxxxxxx or 3xxxxxxxxx ....etc. I thought the System.currentTimeMillis() function returns the number of seconds elapsed since Jan 1970...somethin along that line? When I test that function back to back, like call: System.currentTime(). sleep(5), System.currentTime(). I can see that it gives me a somewhat accurate value. However, when I call it at different machines (because this is a Pnt2Pnt model) the TimeStamp at the other end maybe be 4xxxxxxxxx whiel the TimeStamp at this end is 1xxxxxxxx, How can I compare the the TS of 2 seprate workstation, then?
First, to ask perhaps a really simple questions, the get/set methods, to make them synchronized, all I have to do is add the keyword "synchonized" at their heading and that'll ensure synchronization between different thread access/modify them? So what happens when a thread tries to access a block that is locked by other already? Does it just wait, till it can enter it?
Yes, just add "synchronized". If a thread tries to enter a block that's already locked, then yes, it just waits. It's perfectly transparent to the code that calls the synchronized method.
I thought the System.currentTimeMillis() function returns the number of seconds elapsed since Jan 1970...somethin along that line?
It does: milliseconds since midnight 1 January 1970 GMT. Current values look like 1078864440328 or so. If you're getting values that start with "4" then something's very wrong. If you're sending these over the network, as you imply, then your network code may be wrong.