Patricia Shanahan wrote:
>> I can't work out what is going on, but one obvious problem is that it
>> looks as though some actions on threadsSuspended appear to be outside
[quoted text clipped - 3 lines]
>> whether the event handling thread gets suspended and whether the same
>> object is used for all synchronization that protects the same variable.
> Here is an SSCCE:
>
> import java.awt.*;
> import java.awt.event.*;
> import java.util.Vector;
You might find ArrayList preferable.
> public class DynamicPathGenerator extends Frame implements Runnable {
It's a little unusual to launch an entire Frame (why not a JFrame?) in a
subthread of itself. In your case that is probably not the thing to do.
> /**
Don't use TAB characters in Usenet posts.
> * Member variables
> *
[quoted text clipped - 5 lines]
> Thread clock;
> boolean threadsSuspended = false;
Redundant initialization.
You should consider providing access specification for your instance
variables, almost always "private".
> static final int clock_period = 1000;
Compile time constants conventionally are named in all uppercase letters.
Private? Public?
Code is a little easier to read if you put all static variables together and
all non-static variables together in separate "paragraphs".
> private static final long serialVersionUID = 1L;
> TextArea ta = new TextArea("DynamicPathGenerator ver 1.0\n\n", 10,
[quoted text clipped - 36 lines]
>
> synchronized(this) {
Remember what Thomas Hawtin said, echoing Patricia Shanahan's advice?
>> Note that 'this' within an inner class refers to inner instance, not the outer. Even Brian Goetz managed to publish a book with this mistake.
> notifyAll();
> }
[quoted text clipped - 57 lines]
> public void run()
> {
Why do you need to run a whole Frame in the subthread?
> try {
> while(true)
[quoted text clipped - 3 lines]
> this.counter++;
> this.putText(new Integer(counter).toString());
You ignored the advice about Integer.toString(counter), I see.
> Thread.sleep(clock_period);
> synchronized(this) {
wait() and notify() / notifyAll() have to be called on the same monitor.
> while (threadsSuspended)
> wait();
[quoted text clipped - 4 lines]
>
> return;
You ignored the advice about the redundant "return", I see.
> } // method run
> /**
> * The main method
> */
> public static void main(String argv[]) throws Exception {
Why are you throwing an Exception from main()?
> DynamicPathGenerator dpg = new DynamicPathGenerator();
> dpg.pack();
[quoted text clipped - 3 lines]
>
> } // class DynamicPathGenerator
HTH.

Signature
Lew
Patricia Shanahan - 13 Aug 2007 14:51 GMT
...
>> public static void main(String argv[]) throws Exception {
>
> Why are you throwing an Exception from main()?
I often throw exceptions from main in small test and example programs.
It minimizes the amount of exception processing code that is needed
inside the program, without hiding the exceptions.
Patricia
...
> I am really quite newby with programming threads, so I think I still
> have some basic problems of understanding the concepts of thread
> programming.
...
There is one key concept you are missing. All the wait and notify
actions that you intend to interact MUST use the same object.
You use "this", both implicitly, "wait;", and explicitly,
"synchronized(this)". That does not work, because which object is meant
by "this" depends on where the code is. "this" inside your
ActionListener objects is not the same Object as "this" inside your run
method.
I would declare a threadsSuspensionLock Object right next to the
threadsSuspended declaration. Make ALL synchronization, notify, and wait
actions that relate to threadsSuspended use that object.
synchronized(threadsSuspensionLock)
threadsSuspensionLock.wait()
threadsSuspensionLock.notify()
Obviously, pick a good identifier according to your coding conventions.
Patricia
korcs - 13 Aug 2007 16:49 GMT
> You use "this", both implicitly, "wait;", and explicitly,
> "synchronized(this)". That does not work, because which object is meant
[quoted text clipped - 15 lines]
>
> Patricia
Hey, Patricia thanks a lot it works perfectly!
For the other contributors thanks of course as well.
Best,
korcs