• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java threads: One synchronized, others not?

edit: Sorry, Attach Code is still completely hosed, and for some reason the spaces aren't staying even as a Quote??

I am working with these fairly new Sun SPOT devices which are hella cool and have development tools at least 10x better than the crap that is the Tmotes. I had the choice of either or both, but the Tmote was a huge PITA to even install TinyOS and dev tools, and these Sun SPOTs work so damn hell and the Java implementation is excellent.

Anyway, I've not done threads in Java before so I'm not sure I understand this. The code below was mostly taken from some sample code. Similar sample code that does the nearly the exact same thing, do not have 'synchronized' and are called a little differently.

I didn't include all the code as it is pretty simple and everything seems to be working fine, I'm just curious on why just the Tx thread is 'synchronized' and also is this the proper way to do threads in Java? I think it makes sense to have separate threads for receive and transmit (although they are only to do one either or at a time, selectable by the switch) then have the main thread check the switch for activity. So what is the purpose of 'synchronized' and moreover what is only one of the threads marked as such?

public class Test extends MIDlet
{
protected void startApp() throws MIDletStateChangeException
{
startReceiveThread();
startTransmitThread();
respondToSwitch(); // this thread will monitor switch press
}

private void respondToSwitch()
{
while (true)
{
... // does what name implies
}
}

private void startReceiveThread()
{
new Thread()
{
public void run()
{
while (true)
{
... // receives a radiogram
Utils.sleep(500); // sleep for 500ms
}
}
}.start();
}

synchronized private void startTransmitThread()
{
new Thread()
{
public void run()
{
while (true)
{
... // sends a radiogram
Utils.sleep(500); // sleep for 500ms
}
}
}.start();
}

} // end class
 
A synchronized method means that it is not possible to make two invocations of that method on the same object instance. For example, if a single send object is available to multiple threads, you don't want them using that same send method all at the same time.

EDIT: This is a good read
 
Originally posted by: smack Down
Seems very odd. The synchronized seems to have no effect.

there's synchronized { statement within method of interest that should be used when appropriate.
 
Originally posted by: postmortemIA
Originally posted by: smack Down
Seems very odd. The synchronized seems to have no effect.

there's synchronized { statement within method of interest that should be used when appropriate.

Yeah, so what does that have to do with the op question. There is no point to the synchronized in the ops code because the method doesn't touch any class variables.
 
Originally posted by: smack Down
Originally posted by: postmortemIA
Originally posted by: smack Down
Seems very odd. The synchronized seems to have no effect.

there's synchronized { statement within method of interest that should be used when appropriate.

Yeah, so what does that have to do with the op question. There is no point to the synchronized in the ops code because the method doesn't touch any class variables.

Manipulating class variables is not the only reason a method would be synchronized. 😕
 
Originally posted by: tfinch2
Originally posted by: smack Down
Originally posted by: postmortemIA
Originally posted by: smack Down
Seems very odd. The synchronized seems to have no effect.

there's synchronized { statement within method of interest that should be used when appropriate.

Yeah, so what does that have to do with the op question. There is no point to the synchronized in the ops code because the method doesn't touch any class variables.

Manipulating class variables is not the only reason a method would be synchronized. 😕

Fine, it only manipulates local variables. You happy.
 
Originally posted by: smack Down
Originally posted by: tfinch2
Originally posted by: smack Down
Originally posted by: postmortemIA
Originally posted by: smack Down
Seems very odd. The synchronized seems to have no effect.

there's synchronized { statement within method of interest that should be used when appropriate.

Yeah, so what does that have to do with the op question. There is no point to the synchronized in the ops code because the method doesn't touch any class variables.

Manipulating class variables is not the only reason a method would be synchronized. 😕

Fine, it only manipulates local variables. You happy.

It looks like he didn't paste the full code in the synchronized method. There is a comment as a placeholder though:

"... // sends a radiogram"

I don't know the circumstances of this particular code, but I'd say that's a pretty good reason to have that method synchronized if you only want one radiogram going out at a time, or if the radiogram sender is not thread safe, etc etc.
 
Originally posted by: tfinch2
Originally posted by: smack Down
Originally posted by: tfinch2
Originally posted by: smack Down
Originally posted by: postmortemIA
Originally posted by: smack Down
Seems very odd. The synchronized seems to have no effect.

there's synchronized { statement within method of interest that should be used when appropriate.

Yeah, so what does that have to do with the op question. There is no point to the synchronized in the ops code because the method doesn't touch any class variables.

Manipulating class variables is not the only reason a method would be synchronized. 😕

Fine, it only manipulates local variables. You happy.

It looks like he didn't paste the full code in the synchronized method. There is a comment as a placeholder though:

"... // sends a radiogram"

I don't know the circumstances of this particular code, but I'd say that's a pretty good reason to have that method synchronized if you only want one radiogram going out at a time, or if the radiogram sender is not thread safe, etc etc.

Right but that comment is in the new thread. So that code isn't protected all that is protected is the code that generates the thread which is thread safe anyways.
 
Originally posted by: smack Down
Originally posted by: tfinch2
Originally posted by: smack Down
Originally posted by: tfinch2
Originally posted by: smack Down
Originally posted by: postmortemIA
Originally posted by: smack Down
Seems very odd. The synchronized seems to have no effect.

there's synchronized { statement within method of interest that should be used when appropriate.

Yeah, so what does that have to do with the op question. There is no point to the synchronized in the ops code because the method doesn't touch any class variables.

Manipulating class variables is not the only reason a method would be synchronized. 😕

Fine, it only manipulates local variables. You happy.

It looks like he didn't paste the full code in the synchronized method. There is a comment as a placeholder though:

"... // sends a radiogram"

I don't know the circumstances of this particular code, but I'd say that's a pretty good reason to have that method synchronized if you only want one radiogram going out at a time, or if the radiogram sender is not thread safe, etc etc.

Right but that comment is in the new thread. So that code isn't protected all that is protected is the code that generates the thread which is thread safe anyways.

You're right. I didn't even see that. 😱 😛
 
Back
Top