Home arrow static arrow Java Programming [Archive] - Stack Overflow Error
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Stack Overflow Error
This topic has 62 replies on 5 pages.    « Previous | 1 | 2 | 3 | 4 | 5 |

Posts:179
Registered: 7/30/04
Re: Stack Overflow Error  
Aug 7, 2004 5:00 PM (reply 60 of 62)



 

(quote)
I actually have an event-driven architecture with Observers (which generate events), and Handlers (which process them). The Observers all run in their own threads. After observing events, they send those events to a Handler. In response, the Handler will sometimes create a new Observer (in a new thread). In any case, after the Handler is called the original Observer thread which called it flips a stop switch on its while loop to destroy itself.
(/quote)

Imagine an ObserverQueue class that runs in a single thread. It creates the Observer1 instance, and runs this observer. When the observer observes its event, it will call the Handler. The handler may or may not post a start-the-next-observer message into ObserverQueue. The handler then returns, and so does Observer1.

Execution is now returned to ObserverQueue main loop, which reads the message, creates and runs the next observer. This would create the same observer run sequence as above. It think that this design will make it more clear what is actually going on.
 

Posts:37,103
Registered: 3/30/99
Re: Stack Overflow Error  
Aug 7, 2004 5:22 PM (reply 61 of 62)



 
This leads to the ultimate point that if your custom
class start() method is in fact able to execute the
statement
mThread = new Thread(this);

then the state of the "current runnable" (or the
current thread) must be active. If the current thread
had died, it would not have at all execute the said
statement.

I didn't mean state in the sense of Java's Threads' state. I meant state in the sense of general state of whatever member variables the Runnable itself may have.
public class RunnableStateExample implements Runnable {    private int x_;    private StringBuffer sb_;    private String str_;     public void start() {        new Thread(this).start();    }     public void run() {        // do stuff that messes with x_, sb_, str_    }}
If I create a RunnableStateExample, and then call start() on in multiple times, then you have concurrent, unsynchronized access to an object's state. This is just a general multithreading/synchronization problem.
 

Posts:340
Registered: 7/8/04
Re: Stack Overflow Error  
Aug 7, 2004 6:23 PM (reply 62 of 62)



 
Imagine an ObserverQueue class that runs in a single
thread. It creates the Observer1 instance, and runs
this observer. When the observer observes its event,
it will call the Handler. The handler may or may not
post a start-the-next-observer message into
ObserverQueue. The handler then returns, and so does
Observer1.

Execution is now returned to ObserverQueue main loop,
which reads the message, creates and runs the next
observer. This would create the same observer run
sequence as above. It think that this design will make
it more clear what is actually going on.

I think this a good idea, and actually started thinking about redesigning it this way as soon as I heard that it was a bad idea to create generations of spawned threads, from parents to children to grandchildren, etc. I think this is a good workaround.

John
 
This topic has 62 replies on 5 pages.    « Previous | 1 | 2 | 3 | 4 | 5 |