• Post Reply Bookmark Topic Watch Topic
  • New Topic

New to threads, but what's a better way to do what I need?

 
Joe Vahabzadeh
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I have an Applet running that, for various reasons, I need a thread to keep an eye on a member variable, and take certain action when that variable changes.

This is my first attempt and doing work with threads in quite some time, and the first one with such a dependency.

Here's the code I have, but I'm thinking there's got to be a better, less brute-force way of doing this:



To wit, I'd want the thread to never do any processing unless newItem is a greater-than zero-length string. I'm given to understand that wait and notify can be used for this, but I haven't understood the few examples I've read.

This all seems to work exactly as I need it, but somehow just seems wrong from a code-elegance point of view.

Also, should my addNewItem method (or at least some block of it) be synchronized as well? I believe it should be, but am not really clear on how. I think the only variable I have to be concerned with syncrhonizing is newItem.

Thanks in advance.
[ September 14, 2006: Message edited by: Joe Vahabzadeh ]
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not even in the ball park.

Read about Object.wait() and notify(). There are many examples around on how to use them. Sun has a tutorial on threads. You may want to read it.

If you're using SDK1.5, then read about Condition.await() and signal(). These are the replacements for wait() and notify().
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could do this with wait and notify but why bother?

create a get/setNewItem call setNewItem should fire an event - check out PropertyChangeListener or Observable. If you need to do the work required by the event in another thread - start it then.
 
Joe Vahabzadeh
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim,

Ah, well, there's a caveat in that Javascript is actually making the call to this applet's addNewItem.

Since the applet's jar is signed and trusted, but the Javascript itself isn't, when the call is made to addNewItem by the Javascript, that call is put within the limits of an untrusted app, thus no file reads or writes.

The thread on the other hand was started when the applet's start() function was called, therefore, at that point, everything (including the thread) is trusted, thus we can read and write to the local system's files. That need is why I went with this whole bit of convoluted thread work.



On the other hand, Edward and Tim, do either of you have any recommendations for particular tutorials that deal with wait/notify (or await/signal) that are particularly clear? Normally I go with Sun's tutorials, but I'll have to admit that theirs (as well as those I've found from casual googling has brought me to) had me scratching my head a bit.

(EDIT: mangled parenthetical sentence there)
[ September 14, 2006: Message edited by: Joe Vahabzadeh ]
[ September 15, 2006: Message edited by: Joe Vahabzadeh ]
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main Java.Sun page has links to the Sun tutorials. This is one about concurrency:
http://java.sun.com/docs/books/tutorial/essential/concurrency/
 
Joe Vahabzadeh
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I took a bit more of a look at wait/notify, and I *think* I got the basic gist of it. Now, while that code I had above did what I needed to do, it's clumsy.

I tested the following code, and it works, but I want to get your opinions on whether it's correct from a technique point of view, or if not, what I'm doing wrong or misunderstanding.


Thanks again guys.

EDIT: Admittedly, I'm a little uncomfortable sticking "synchronized" on the Applet's "stop" method, so I was considering this instead, but didn't know if it was necessary:


[ September 18, 2006: Message edited by: Joe Vahabzadeh ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!