Question about testing DefaultActor in Java

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Question about testing DefaultActor in Java

Eric MacAdie
I am a Java developer looking at GPars.

I have a question about extending DefaultActor in Java. Here is a snippet from the test samples in the sample Maven project, from the class org.codehaus.gpars.javademo.StatefulActorTest:
        protected void act() {

            loop(new Runnable() {

                // Run will be executed with first message sent to actor
                @Override
                public void run() {

                    // Schedule process to retrieve second message from queue and MessagingRunnable to process it
                    react(new MessagingRunnable<Object>(this) {
                        @Override
                        protected void doRun(final Object s) {
                            System.out.println("Received in react: " + s);

                            // Schedule process to retrieve third message from queue and MessagingRunnable to process it
                            react(new MessagingRunnable<String>() {
                                @Override
                                protected void doRun(final String s) {
                                    System.out.println("Received in nested react: " + s);

                                    react(new MessagingRunnable<Integer>() {
                                        @Override
                                        protected void doRun(final Integer integer) {
                                            System.out.println("Received a number in nested react: " + integer);
                                        }
                                    });
                                }
                            });
                        }
                    });
                }
            });
        }

If you wanted to handle different types of messages, do you need to nest that deeply? Looking through the docs, I think I may have found a cleaner way. I was wondering if it is still thread-safe:

public class MyActor extends DefaultActor {

    private int counter = 0;
    private int stringCounter = 0;
    private int intCounter = 0;
    private int otherCounter = 0;

    public int getCounter(){ return counter; }
    public int getStringCounter(){ return stringCounter; }
    public int getIntCounter(){ return intCounter; }
    public int getOtherCounter(){ return otherCounter; }

    @Override
    protected void act() {

	loop(new Runnable() {

		// Run will be executed with first message sent to actor
            @Override
	    public void run() {

		// Schedule process to retrieve second message from queue and MessagingRunnable to process it
		react(new MessagingRunnable<Object>(this) {
                    @Override
		    protected void doRun(final Object s) {
			counter++;
			System.out.println( "\nReceived in react: " + s +
			    " s is a " + s.getClass().getName() +
			    " in " + this.hashCode() + " in thread " +
			    Thread.currentThread().getName() );
			if (s instanceof String) {
			    stringCounter++;
			    System.out.println( "s is a String: " + s );
			} else if (s instanceof Integer) {
			    intCounter++;
			    System.out.println( "s is an Integer: " + s );
			} else if (s instanceof Object) {
			    System.out.println( "s is a Object: " + s );
			    otherCounter++;
			}
			// perform if (s instanceof here)
                        
		    }
		    });
            } // end run
	});
    } // end act
} // end class

             

Would the second way still be thread-safe?
= Eric MacAdie


Reply | Threaded
Open this post in threaded view
|

Re: Question about testing DefaultActor in Java

Vaclav
Administrator
Hi Eric,

to your question - yes, I think your code is still thread safe, except for the public getters. They would bypass the actor asynchronous invocation mechanism, since they can be called directly (and synchronously) and access unsynchronized private fields. Retrieving the field values must be done through message passing, as well.

Your example, however, has different semantics from the original one, which only accepted a given sequence of messages, while yours can accept any of the three types of messages in any order and quantity. For such scenarios, the DynamicDispatchActor is a better choice, in my opinion.

Cheers,

Vaclav




On Wed, Jul 24, 2013 at 4:12 PM, Eric MacAdie <[hidden email]> wrote:
I am a Java developer looking at GPars.

I have a question about extending DefaultActor in Java. Here is a snippet from the test samples in the sample Maven project, from the class org.codehaus.gpars.javademo.StatefulActorTest:
        protected void act() {

            loop(new Runnable() {

                // Run will be executed with first message sent to actor
                @Override
                public void run() {

                    // Schedule process to retrieve second message from queue and MessagingRunnable to process it
                    react(new MessagingRunnable<Object>(this) {
                        @Override
                        protected void doRun(final Object s) {
                            System.out.println("Received in react: " + s);

                            // Schedule process to retrieve third message from queue and MessagingRunnable to process it
                            react(new MessagingRunnable<String>() {
                                @Override
                                protected void doRun(final String s) {
                                    System.out.println("Received in nested react: " + s);

                                    react(new MessagingRunnable<Integer>() {
                                        @Override
                                        protected void doRun(final Integer integer) {
                                            System.out.println("Received a number in nested react: " + integer);
                                        }
                                    });
                                }
                            });
                        }
                    });
                }
            });
        }

If you wanted to handle different types of messages, do you need to nest that deeply? Looking through the docs, I think I may have found a cleaner way. I was wondering if it is still thread-safe:

public class MyActor extends DefaultActor {

    private int counter = 0;
    private int stringCounter = 0;
    private int intCounter = 0;
    private int otherCounter = 0;

    public int getCounter(){ return counter; }
    public int getStringCounter(){ return stringCounter; }
    public int getIntCounter(){ return intCounter; }
    public int getOtherCounter(){ return otherCounter; }

    @Override
    protected void act() {

	loop(new Runnable() {

		// Run will be executed with first message sent to actor
            @Override
	    public void run() {

		// Schedule process to retrieve second message from queue and MessagingRunnable to process it
		react(new MessagingRunnable<Object>(this) {
                    @Override
		    protected void doRun(final Object s) {
			counter++;
			System.out.println( "\nReceived in react: " + s +
			    " s is a " + s.getClass().getName() +
			    " in " + this.hashCode() + " in thread " +
			    Thread.currentThread().getName() );
			if (s instanceof String) {
			    stringCounter++;
			    System.out.println( "s is a String: " + s );
			} else if (s instanceof Integer) {
			    intCounter++;
			    System.out.println( "s is an Integer: " + s );
			} else if (s instanceof Object) {
			    System.out.println( "s is a Object: " + s );
			    otherCounter++;
			}
			// perform if (s instanceof here)
                        
		    }
		    });
            } // end run
	});
    } // end act
} // end class

             

Would the second way still be thread-safe?
= Eric MacAdie





--
E-mail: [hidden email]
Blog: http://www.jroller.com/vaclav
Linkedin page: http://www.linkedin.com/in/vaclavpech
Reply | Threaded
Open this post in threaded view
|

Re: Question about testing DefaultActor in Java

Eric MacAdie
Thanks for the response.

I prefer being able to handle messages that are sent in any order. That is why I wanted something closer to the DynamicDispatchActor. The one issue with DynamicDispatchActor is that it is stateless. I would like to be able to have a stateful actor that is a bit cleaner than the example in the sample project. The "Types of actors" section of the Actors page in the docs mentions an actor holding data between messages. I am under the impression that DynamicDispatchActor would not work for that.

= Eric MacAdie

On Wed, Jul 24, 2013 at 10:28 AM, Václav Pech <[hidden email]> wrote:
Hi Eric,

to your question - yes, I think your code is still thread safe, except for the public getters. They would bypass the actor asynchronous invocation mechanism, since they can be called directly (and synchronously) and access unsynchronized private fields. Retrieving the field values must be done through message passing, as well.

Your example, however, has different semantics from the original one, which only accepted a given sequence of messages, while yours can accept any of the three types of messages in any order and quantity. For such scenarios, the DynamicDispatchActor is a better choice, in my opinion.

Cheers,

Vaclav




On Wed, Jul 24, 2013 at 4:12 PM, Eric MacAdie <[hidden email]> wrote:
I am a Java developer looking at GPars.

I have a question about extending DefaultActor in Java. Here is a snippet from the test samples in the sample Maven project, from the class org.codehaus.gpars.javademo.StatefulActorTest:
        protected void act() {

            loop(new Runnable() {

                // Run will be executed with first message sent to actor
                @Override
                public void run() {

                    // Schedule process to retrieve second message from queue and MessagingRunnable to process it
                    react(new MessagingRunnable<Object>(this) {
                        @Override
                        protected void doRun(final Object s) {
                            System.out.println("Received in react: " + s);

                            // Schedule process to retrieve third message from queue and MessagingRunnable to process it
                            react(new MessagingRunnable<String>() {
                                @Override
                                protected void doRun(final String s) {
                                    System.out.println("Received in nested react: " + s);

                                    react(new MessagingRunnable<Integer>() {
                                        @Override
                                        protected void doRun(final Integer integer) {
                                            System.out.println("Received a number in nested react: " + integer);
                                        }
                                    });
                                }
                            });
                        }
                    });
                }
            });
        }

If you wanted to handle different types of messages, do you need to nest that deeply? Looking through the docs, I think I may have found a cleaner way. I was wondering if it is still thread-safe:

public class MyActor extends DefaultActor {

    private int counter = 0;
    private int stringCounter = 0;
    private int intCounter = 0;
    private int otherCounter = 0;

    public int getCounter(){ return counter; }
    public int getStringCounter(){ return stringCounter; }
    public int getIntCounter(){ return intCounter; }
    public int getOtherCounter(){ return otherCounter; }

    @Override
    protected void act() {

	loop(new Runnable() {

		// Run will be executed with first message sent to actor
            @Override
	    public void run() {

		// Schedule process to retrieve second message from queue and MessagingRunnable to process it
		react(new MessagingRunnable<Object>(this) {
                    @Override
		    protected void doRun(final Object s) {
			counter++;
			System.out.println( "\nReceived in react: " + s +
			    " s is a " + s.getClass().getName() +
			    " in " + this.hashCode() + " in thread " +
			    Thread.currentThread().getName() );
			if (s instanceof String) {
			    stringCounter++;
			    System.out.println( "s is a String: " + s );
			} else if (s instanceof Integer) {
			    intCounter++;
			    System.out.println( "s is an Integer: " + s );
			} else if (s instanceof Object) {
			    System.out.println( "s is a Object: " + s );
			    otherCounter++;
			}
			// perform if (s instanceof here)
                        
		    }
		    });
            } // end run
	});
    } // end act
} // end class

             

Would the second way still be thread-safe?
= Eric MacAdie





--
E-mail: [hidden email]
Blog: http://www.jroller.com/vaclav
Linkedin page: http://www.linkedin.com/in/vaclavpech

Reply | Threaded
Open this post in threaded view
|

Re: Question about testing DefaultActor in Java

Vaclav
Administrator
No, worries about the state. DynamicDispatchActor can have private fields just like other actors can. I see, it is the "stateless" in the name of the tutorial chapter that causes confusion, but it comes from the fact that DynamicDispatchActor, unlike DefaultActor, does not remember the history of received messages and so cannot easily implement protocols - i.e. an expected sequence of incoming messages implemented through a series of reacts within DefaultActor's body.

DynamicDispatchActor has no limitations on remembering its private fields values.

Vaclav



On Thu, Jul 25, 2013 at 5:58 AM, Eric MacAdie <[hidden email]> wrote:
Thanks for the response.

I prefer being able to handle messages that are sent in any order. That is why I wanted something closer to the DynamicDispatchActor. The one issue with DynamicDispatchActor is that it is stateless. I would like to be able to have a stateful actor that is a bit cleaner than the example in the sample project. The "Types of actors" section of the Actors page in the docs mentions an actor holding data between messages. I am under the impression that DynamicDispatchActor would not work for that.

= Eric MacAdie


On Wed, Jul 24, 2013 at 10:28 AM, Václav Pech <[hidden email]> wrote:
Hi Eric,

to your question - yes, I think your code is still thread safe, except for the public getters. They would bypass the actor asynchronous invocation mechanism, since they can be called directly (and synchronously) and access unsynchronized private fields. Retrieving the field values must be done through message passing, as well.

Your example, however, has different semantics from the original one, which only accepted a given sequence of messages, while yours can accept any of the three types of messages in any order and quantity. For such scenarios, the DynamicDispatchActor is a better choice, in my opinion.

Cheers,

Vaclav




On Wed, Jul 24, 2013 at 4:12 PM, Eric MacAdie <[hidden email]> wrote:
I am a Java developer looking at GPars.

I have a question about extending DefaultActor in Java. Here is a snippet from the test samples in the sample Maven project, from the class org.codehaus.gpars.javademo.StatefulActorTest:
        protected void act() {

            loop(new Runnable() {

                // Run will be executed with first message sent to actor
                @Override
                public void run() {

                    // Schedule process to retrieve second message from queue and MessagingRunnable to process it
                    react(new MessagingRunnable<Object>(this) {
                        @Override
                        protected void doRun(final Object s) {
                            System.out.println("Received in react: " + s);

                            // Schedule process to retrieve third message from queue and MessagingRunnable to process it
                            react(new MessagingRunnable<String>() {
                                @Override
                                protected void doRun(final String s) {
                                    System.out.println("Received in nested react: " + s);

                                    react(new MessagingRunnable<Integer>() {
                                        @Override
                                        protected void doRun(final Integer integer) {
                                            System.out.println("Received a number in nested react: " + integer);
                                        }
                                    });
                                }
                            });
                        }
                    });
                }
            });
        }

If you wanted to handle different types of messages, do you need to nest that deeply? Looking through the docs, I think I may have found a cleaner way. I was wondering if it is still thread-safe:

public class MyActor extends DefaultActor {

    private int counter = 0;
    private int stringCounter = 0;
    private int intCounter = 0;
    private int otherCounter = 0;

    public int getCounter(){ return counter; }
    public int getStringCounter(){ return stringCounter; }
    public int getIntCounter(){ return intCounter; }
    public int getOtherCounter(){ return otherCounter; }

    @Override
    protected void act() {

	loop(new Runnable() {

		// Run will be executed with first message sent to actor
            @Override
	    public void run() {

		// Schedule process to retrieve second message from queue and MessagingRunnable to process it
		react(new MessagingRunnable<Object>(this) {
                    @Override
		    protected void doRun(final Object s) {
			counter++;
			System.out.println( "\nReceived in react: " + s +
			    " s is a " + s.getClass().getName() +
			    " in " + this.hashCode() + " in thread " +
			    Thread.currentThread().getName() );
			if (s instanceof String) {
			    stringCounter++;
			    System.out.println( "s is a String: " + s );
			} else if (s instanceof Integer) {
			    intCounter++;
			    System.out.println( "s is an Integer: " + s );
			} else if (s instanceof Object) {
			    System.out.println( "s is a Object: " + s );
			    otherCounter++;
			}
			// perform if (s instanceof here)
                        
		    }
		    });
            } // end run
	});
    } // end act
} // end class

             

Would the second way still be thread-safe?
= Eric MacAdie





--
E-mail: [hidden email]
Blog: http://www.jroller.com/vaclav
Linkedin page: http://www.linkedin.com/in/vaclavpech




--
E-mail: [hidden email]
Blog: http://www.jroller.com/vaclav
Linkedin page: http://www.linkedin.com/in/vaclavpech
Reply | Threaded
Open this post in threaded view
|

Re: Question about testing DefaultActor in Java

Eric MacAdie
I have another question about the DynamicDispatchActor: Suppose my onMessage(SomeObject s) calls another method, since that can be cleaner than a looooong method:

   public void onMessage(final Object msg) {
      // other stuff
      y = this.someOtherMethod(x);
      // more stuff
   }
  
   public Something someOtherMethod( Thing x ) {
       // do stuff
   }

Is this also thread-safe? I am guessing that it is, but I thought I would ask first.

= Eric MacAdie




On Thu, Jul 25, 2013 at 5:33 AM, Václav Pech <[hidden email]> wrote:
No, worries about the state. DynamicDispatchActor can have private fields just like other actors can. I see, it is the "stateless" in the name of the tutorial chapter that causes confusion, but it comes from the fact that DynamicDispatchActor, unlike DefaultActor, does not remember the history of received messages and so cannot easily implement protocols - i.e. an expected sequence of incoming messages implemented through a series of reacts within DefaultActor's body.

DynamicDispatchActor has no limitations on remembering its private fields values.

Vaclav



On Thu, Jul 25, 2013 at 5:58 AM, Eric MacAdie <[hidden email]> wrote:
Thanks for the response.

I prefer being able to handle messages that are sent in any order. That is why I wanted something closer to the DynamicDispatchActor. The one issue with DynamicDispatchActor is that it is stateless. I would like to be able to have a stateful actor that is a bit cleaner than the example in the sample project. The "Types of actors" section of the Actors page in the docs mentions an actor holding data between messages. I am under the impression that DynamicDispatchActor would not work for that.

= Eric MacAdie


On Wed, Jul 24, 2013 at 10:28 AM, Václav Pech <[hidden email]> wrote:
Hi Eric,

to your question - yes, I think your code is still thread safe, except for the public getters. They would bypass the actor asynchronous invocation mechanism, since they can be called directly (and synchronously) and access unsynchronized private fields. Retrieving the field values must be done through message passing, as well.

Your example, however, has different semantics from the original one, which only accepted a given sequence of messages, while yours can accept any of the three types of messages in any order and quantity. For such scenarios, the DynamicDispatchActor is a better choice, in my opinion.

Cheers,

Vaclav




On Wed, Jul 24, 2013 at 4:12 PM, Eric MacAdie <[hidden email]> wrote:
I am a Java developer looking at GPars.

I have a question about extending DefaultActor in Java. Here is a snippet from the test samples in the sample Maven project, from the class org.codehaus.gpars.javademo.StatefulActorTest:
        protected void act() {

            loop(new Runnable() {

                // Run will be executed with first message sent to actor
                @Override
                public void run() {

                    // Schedule process to retrieve second message from queue and MessagingRunnable to process it
                    react(new MessagingRunnable<Object>(this) {
                        @Override
                        protected void doRun(final Object s) {
                            System.out.println("Received in react: " + s);

                            // Schedule process to retrieve third message from queue and MessagingRunnable to process it
                            react(new MessagingRunnable<String>() {
                                @Override
                                protected void doRun(final String s) {
                                    System.out.println("Received in nested react: " + s);

                                    react(new MessagingRunnable<Integer>() {
                                        @Override
                                        protected void doRun(final Integer integer) {
                                            System.out.println("Received a number in nested react: " + integer);
                                        }
                                    });
                                }
                            });
                        }
                    });
                }
            });
        }

If you wanted to handle different types of messages, do you need to nest that deeply? Looking through the docs, I think I may have found a cleaner way. I was wondering if it is still thread-safe:

public class MyActor extends DefaultActor {

    private int counter = 0;
    private int stringCounter = 0;
    private int intCounter = 0;
    private int otherCounter = 0;

    public int getCounter(){ return counter; }
    public int getStringCounter(){ return stringCounter; }
    public int getIntCounter(){ return intCounter; }
    public int getOtherCounter(){ return otherCounter; }

    @Override
    protected void act() {

	loop(new Runnable() {

		// Run will be executed with first message sent to actor
            @Override
	    public void run() {

		// Schedule process to retrieve second message from queue and MessagingRunnable to process it
		react(new MessagingRunnable<Object>(this) {
                    @Override
		    protected void doRun(final Object s) {
			counter++;
			System.out.println( "\nReceived in react: " + s +
			    " s is a " + s.getClass().getName() +
			    " in " + this.hashCode() + " in thread " +
			    Thread.currentThread().getName() );
			if (s instanceof String) {
			    stringCounter++;
			    System.out.println( "s is a String: " + s );
			} else if (s instanceof Integer) {
			    intCounter++;
			    System.out.println( "s is an Integer: " + s );
			} else if (s instanceof Object) {
			    System.out.println( "s is a Object: " + s );
			    otherCounter++;
			}
			// perform if (s instanceof here)
                        
		    }
		    });
            } // end run
	});
    } // end act
} // end class

             

Would the second way still be thread-safe?
= Eric MacAdie





--
E-mail: [hidden email]
Blog: http://www.jroller.com/vaclav
Linkedin page: http://www.linkedin.com/in/vaclavpech




--
E-mail: [hidden email]
Blog: http://www.jroller.com/vaclav
Linkedin page: http://www.linkedin.com/in/vaclavpech

Reply | Threaded
Open this post in threaded view
|

Re: Question about testing DefaultActor in Java

Dierk König
it is only safe as long as no other caller can directly call into someOtherMethod and for that reason it is better to declare it private (even though that is no 100% guarantee).

cheers
Dierk

Am 16.08.2013 um 06:11 schrieb Eric MacAdie <[hidden email]>:

> I have another question about the DynamicDispatchActor: Suppose my onMessage(SomeObject s) calls another method, since that can be cleaner than a looooong method:
>
>    public void onMessage(final Object msg) {
>       // other stuff
>       y = this.someOtherMethod(x);
>       // more stuff
>    }
>    
>    public Something someOtherMethod( Thing x ) {
>        // do stuff
>    }
>
> Is this also thread-safe? I am guessing that it is, but I thought I would ask first.
>
> = Eric MacAdie
>
>
>
>
> On Thu, Jul 25, 2013 at 5:33 AM, Václav Pech <[hidden email]> wrote:
> No, worries about the state. DynamicDispatchActor can have private fields just like other actors can. I see, it is the "stateless" in the name of the tutorial chapter that causes confusion, but it comes from the fact that DynamicDispatchActor, unlike DefaultActor, does not remember the history of received messages and so cannot easily implement protocols - i.e. an expected sequence of incoming messages implemented through a series of reacts within DefaultActor's body.
>
> DynamicDispatchActor has no limitations on remembering its private fields values.
>
> Vaclav
>
>
>
> On Thu, Jul 25, 2013 at 5:58 AM, Eric MacAdie <[hidden email]> wrote:
> Thanks for the response.
>
> I prefer being able to handle messages that are sent in any order. That is why I wanted something closer to the DynamicDispatchActor. The one issue with DynamicDispatchActor is that it is stateless. I would like to be able to have a stateful actor that is a bit cleaner than the example in the sample project. The "Types of actors" section of the Actors page in the docs mentions an actor holding data between messages. I am under the impression that DynamicDispatchActor would not work for that.
>
> = Eric MacAdie
>
>
> On Wed, Jul 24, 2013 at 10:28 AM, Václav Pech <[hidden email]> wrote:
> Hi Eric,
>
> to your question - yes, I think your code is still thread safe, except for the public getters. They would bypass the actor asynchronous invocation mechanism, since they can be called directly (and synchronously) and access unsynchronized private fields. Retrieving the field values must be done through message passing, as well.
>
> Your example, however, has different semantics from the original one, which only accepted a given sequence of messages, while yours can accept any of the three types of messages in any order and quantity. For such scenarios, the DynamicDispatchActor is a better choice, in my opinion.
>
> Cheers,
>
> Vaclav
>
>
>
>
> On Wed, Jul 24, 2013 at 4:12 PM, Eric MacAdie <[hidden email]> wrote:
> I am a Java developer looking at GPars.
>
> I have a question about extending DefaultActor in Java. Here is a snippet from the test samples in the sample Maven project, from the class org.codehaus.gpars.javademo.StatefulActorTest:
>         protected void act() {
>
>             loop(new Runnable() {
>
>                 // Run will be executed with first message sent to actor
>                 @Override
>                 public void run() {
>
>                     // Schedule process to retrieve second message from queue and MessagingRunnable to process it
>                     react(new MessagingRunnable<Object>(this) {
>                         @Override
>                         protected void doRun(final Object s) {
>                             System.out.println("Received in react: " + s);
>
>                             // Schedule process to retrieve third message from queue and MessagingRunnable to process it
>                             react(new MessagingRunnable<String>() {
>                                 @Override
>                                 protected void doRun(final String s) {
>                                     System.out.println("Received in nested react: " + s);
>
>                                     react(new MessagingRunnable<Integer>() {
>                                         @Override
>                                         protected void doRun(final Integer integer) {
>                                             System.out.println("Received a number in nested react: " + integer);
>                                         }
>                                     });
>                                 }
>                             });
>                         }
>                     });
>                 }
>             });
>         }
>
> If you wanted to handle different types of messages, do you need to nest that deeply? Looking through the docs, I think I may have found a cleaner way. I was wondering if it is still thread-safe:
>
> public class MyActor extends DefaultActor {
>
>     private int counter = 0;
>     private int stringCounter = 0;
>     private int intCounter = 0;
>     private int otherCounter = 0;
>
>     public int getCounter(){ return counter; }
>     public int getStringCounter(){ return stringCounter; }
>     public int getIntCounter(){ return intCounter; }
>     public int getOtherCounter(){ return otherCounter; }
>
>     @Override
>     protected void act() {
>
> loop(new Runnable() {
>
> // Run will be executed with first message sent to actor
>             @Override
>    public void run() {
>
> // Schedule process to retrieve second message from queue and MessagingRunnable to process it
> react(new MessagingRunnable<Object>(this) {
>                     @Override
>    protected void doRun(final Object s) {
> counter++;
> System.out.println( "\nReceived in react: " + s +
>    " s is a " + s.getClass().getName() +
>    " in " + this.hashCode() + " in thread " +
>    Thread.currentThread().getName() );
> if (s instanceof String) {
>    stringCounter++;
>    System.out.println( "s is a String: " + s );
> } else if (s instanceof Integer) {
>    intCounter++;
>    System.out.println( "s is an Integer: " + s );
> } else if (s instanceof Object) {
>    System.out.println( "s is a Object: " + s );
>    otherCounter++;
> }
> // perform if (s instanceof here)
>                        
>    }
>    });
>             } // end run
> });
>     } // end act
> } // end class
>
>
>              
>
> Would the second way still be thread-safe?
> = Eric MacAdie
>
>
>
>
>
> --
> E-mail: [hidden email]
> Blog: http://www.jroller.com/vaclav
> Linkedin page: http://www.linkedin.com/in/vaclavpech
>
>
>
>
> --
> E-mail: [hidden email]
> Blog: http://www.jroller.com/vaclav
> Linkedin page: http://www.linkedin.com/in/vaclavpech
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Question about testing DefaultActor in Java

Eric MacAdie
Thanks for the info.

Yes, that other method should have been private.

I may have more questions soon. The local Groovy/Grails group is looking for speakers. I recently went through Venkat S's book "Programming Concurrency on the JVM", and I might try to put something together comparing Akka and GPars.

= Eric MacAdie



On Fri, Aug 16, 2013 at 8:23 AM, Dierk König <[hidden email]> wrote:
it is only safe as long as no other caller can directly call into someOtherMethod and for that reason it is better to declare it private (even though that is no 100% guarantee).

cheers
Dierk

Am 16.08.2013 um 06:11 schrieb Eric MacAdie <[hidden email]>:

> I have another question about the DynamicDispatchActor: Suppose my onMessage(SomeObject s) calls another method, since that can be cleaner than a looooong method:
>
>    public void onMessage(final Object msg) {
>       // other stuff
>       y = this.someOtherMethod(x);
>       // more stuff
>    }
>
>    public Something someOtherMethod( Thing x ) {
>        // do stuff
>    }
>
> Is this also thread-safe? I am guessing that it is, but I thought I would ask first.
>
> = Eric MacAdie
>
>
>
>
> On Thu, Jul 25, 2013 at 5:33 AM, Václav Pech <[hidden email]> wrote:
> No, worries about the state. DynamicDispatchActor can have private fields just like other actors can. I see, it is the "stateless" in the name of the tutorial chapter that causes confusion, but it comes from the fact that DynamicDispatchActor, unlike DefaultActor, does not remember the history of received messages and so cannot easily implement protocols - i.e. an expected sequence of incoming messages implemented through a series of reacts within DefaultActor's body.
>
> DynamicDispatchActor has no limitations on remembering its private fields values.
>
> Vaclav
>
>
>
> On Thu, Jul 25, 2013 at 5:58 AM, Eric MacAdie <[hidden email]> wrote:
> Thanks for the response.
>
> I prefer being able to handle messages that are sent in any order. That is why I wanted something closer to the DynamicDispatchActor. The one issue with DynamicDispatchActor is that it is stateless. I would like to be able to have a stateful actor that is a bit cleaner than the example in the sample project. The "Types of actors" section of the Actors page in the docs mentions an actor holding data between messages. I am under the impression that DynamicDispatchActor would not work for that.
>
> = Eric MacAdie
>
>
> On Wed, Jul 24, 2013 at 10:28 AM, Václav Pech <[hidden email]> wrote:
> Hi Eric,
>
> to your question - yes, I think your code is still thread safe, except for the public getters. They would bypass the actor asynchronous invocation mechanism, since they can be called directly (and synchronously) and access unsynchronized private fields. Retrieving the field values must be done through message passing, as well.
>
> Your example, however, has different semantics from the original one, which only accepted a given sequence of messages, while yours can accept any of the three types of messages in any order and quantity. For such scenarios, the DynamicDispatchActor is a better choice, in my opinion.
>
> Cheers,
>
> Vaclav
>
>
>
>
> On Wed, Jul 24, 2013 at 4:12 PM, Eric MacAdie <[hidden email]> wrote:
> I am a Java developer looking at GPars.
>
> I have a question about extending DefaultActor in Java. Here is a snippet from the test samples in the sample Maven project, from the class org.codehaus.gpars.javademo.StatefulActorTest:
>         protected void act() {
>
>             loop(new Runnable() {
>
>                 // Run will be executed with first message sent to actor
>                 @Override
>                 public void run() {
>
>                     // Schedule process to retrieve second message from queue and MessagingRunnable to process it
>                     react(new MessagingRunnable<Object>(this) {
>                         @Override
>                         protected void doRun(final Object s) {
>                             System.out.println("Received in react: " + s);
>
>                             // Schedule process to retrieve third message from queue and MessagingRunnable to process it
>                             react(new MessagingRunnable<String>() {
>                                 @Override
>                                 protected void doRun(final String s) {
>                                     System.out.println("Received in nested react: " + s);
>
>                                     react(new MessagingRunnable<Integer>() {
>                                         @Override
>                                         protected void doRun(final Integer integer) {
>                                             System.out.println("Received a number in nested react: " + integer);
>                                         }
>                                     });
>                                 }
>                             });
>                         }
>                     });
>                 }
>             });
>         }
>
> If you wanted to handle different types of messages, do you need to nest that deeply? Looking through the docs, I think I may have found a cleaner way. I was wondering if it is still thread-safe:
>
> public class MyActor extends DefaultActor {
>
>     private int counter = 0;
>     private int stringCounter = 0;
>     private int intCounter = 0;
>     private int otherCounter = 0;
>
>     public int getCounter(){ return counter; }
>     public int getStringCounter(){ return stringCounter; }
>     public int getIntCounter(){ return intCounter; }
>     public int getOtherCounter(){ return otherCounter; }
>
>     @Override
>     protected void act() {
>
>       loop(new Runnable() {
>
>               // Run will be executed with first message sent to actor
>             @Override
>           public void run() {
>
>               // Schedule process to retrieve second message from queue and MessagingRunnable to process it
>               react(new MessagingRunnable<Object>(this) {
>                     @Override
>                   protected void doRun(final Object s) {
>                       counter++;
>                       System.out.println( "\nReceived in react: " + s +
>                           " s is a " + s.getClass().getName() +
>                           " in " + this.hashCode() + " in thread " +
>                           Thread.currentThread().getName() );
>                       if (s instanceof String) {
>                           stringCounter++;
>                           System.out.println( "s is a String: " + s );
>                       } else if (s instanceof Integer) {
>                           intCounter++;
>                           System.out.println( "s is an Integer: " + s );
>                       } else if (s instanceof Object) {
>                           System.out.println( "s is a Object: " + s );
>                           otherCounter++;
>                       }
>                       // perform if (s instanceof here)
>
>                   }
>                   });
>             } // end run
>       });
>     } // end act
> } // end class
>
>
>
>
> Would the second way still be thread-safe?
> = Eric MacAdie
>
>
>
>
>
> --
> E-mail: [hidden email]
> Blog: http://www.jroller.com/vaclav
> Linkedin page: http://www.linkedin.com/in/vaclavpech
>
>
>
>
> --
> E-mail: [hidden email]
> Blog: http://www.jroller.com/vaclav
> Linkedin page: http://www.linkedin.com/in/vaclavpech
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: Question about testing DefaultActor in Java

Dierk König
Perfect. Go for it!

cheers
Dierk

Am 20.08.2013 um 06:28 schrieb Eric MacAdie <[hidden email]>:

> Thanks for the info.
>
> Yes, that other method should have been private.
>
> I may have more questions soon. The local Groovy/Grails group is looking for speakers. I recently went through Venkat S's book "Programming Concurrency on the JVM", and I might try to put something together comparing Akka and GPars.
>
> = Eric MacAdie
>
>
>
> On Fri, Aug 16, 2013 at 8:23 AM, Dierk König <[hidden email]> wrote:
> it is only safe as long as no other caller can directly call into someOtherMethod and for that reason it is better to declare it private (even though that is no 100% guarantee).
>
> cheers
> Dierk
>
> Am 16.08.2013 um 06:11 schrieb Eric MacAdie <[hidden email]>:
>
> > I have another question about the DynamicDispatchActor: Suppose my onMessage(SomeObject s) calls another method, since that can be cleaner than a looooong method:
> >
> >    public void onMessage(final Object msg) {
> >       // other stuff
> >       y = this.someOtherMethod(x);
> >       // more stuff
> >    }
> >
> >    public Something someOtherMethod( Thing x ) {
> >        // do stuff
> >    }
> >
> > Is this also thread-safe? I am guessing that it is, but I thought I would ask first.
> >
> > = Eric MacAdie
> >
> >
> >
> >
> > On Thu, Jul 25, 2013 at 5:33 AM, Václav Pech <[hidden email]> wrote:
> > No, worries about the state. DynamicDispatchActor can have private fields just like other actors can. I see, it is the "stateless" in the name of the tutorial chapter that causes confusion, but it comes from the fact that DynamicDispatchActor, unlike DefaultActor, does not remember the history of received messages and so cannot easily implement protocols - i.e. an expected sequence of incoming messages implemented through a series of reacts within DefaultActor's body.
> >
> > DynamicDispatchActor has no limitations on remembering its private fields values.
> >
> > Vaclav
> >
> >
> >
> > On Thu, Jul 25, 2013 at 5:58 AM, Eric MacAdie <[hidden email]> wrote:
> > Thanks for the response.
> >
> > I prefer being able to handle messages that are sent in any order. That is why I wanted something closer to the DynamicDispatchActor. The one issue with DynamicDispatchActor is that it is stateless. I would like to be able to have a stateful actor that is a bit cleaner than the example in the sample project. The "Types of actors" section of the Actors page in the docs mentions an actor holding data between messages. I am under the impression that DynamicDispatchActor would not work for that.
> >
> > = Eric MacAdie
> >
> >
> > On Wed, Jul 24, 2013 at 10:28 AM, Václav Pech <[hidden email]> wrote:
> > Hi Eric,
> >
> > to your question - yes, I think your code is still thread safe, except for the public getters. They would bypass the actor asynchronous invocation mechanism, since they can be called directly (and synchronously) and access unsynchronized private fields. Retrieving the field values must be done through message passing, as well.
> >
> > Your example, however, has different semantics from the original one, which only accepted a given sequence of messages, while yours can accept any of the three types of messages in any order and quantity. For such scenarios, the DynamicDispatchActor is a better choice, in my opinion.
> >
> > Cheers,
> >
> > Vaclav
> >
> >
> >
> >
> > On Wed, Jul 24, 2013 at 4:12 PM, Eric MacAdie <[hidden email]> wrote:
> > I am a Java developer looking at GPars.
> >
> > I have a question about extending DefaultActor in Java. Here is a snippet from the test samples in the sample Maven project, from the class org.codehaus.gpars.javademo.StatefulActorTest:
> >         protected void act() {
> >
> >             loop(new Runnable() {
> >
> >                 // Run will be executed with first message sent to actor
> >                 @Override
> >                 public void run() {
> >
> >                     // Schedule process to retrieve second message from queue and MessagingRunnable to process it
> >                     react(new MessagingRunnable<Object>(this) {
> >                         @Override
> >                         protected void doRun(final Object s) {
> >                             System.out.println("Received in react: " + s);
> >
> >                             // Schedule process to retrieve third message from queue and MessagingRunnable to process it
> >                             react(new MessagingRunnable<String>() {
> >                                 @Override
> >                                 protected void doRun(final String s) {
> >                                     System.out.println("Received in nested react: " + s);
> >
> >                                     react(new MessagingRunnable<Integer>() {
> >                                         @Override
> >                                         protected void doRun(final Integer integer) {
> >                                             System.out.println("Received a number in nested react: " + integer);
> >                                         }
> >                                     });
> >                                 }
> >                             });
> >                         }
> >                     });
> >                 }
> >             });
> >         }
> >
> > If you wanted to handle different types of messages, do you need to nest that deeply? Looking through the docs, I think I may have found a cleaner way. I was wondering if it is still thread-safe:
> >
> > public class MyActor extends DefaultActor {
> >
> >     private int counter = 0;
> >     private int stringCounter = 0;
> >     private int intCounter = 0;
> >     private int otherCounter = 0;
> >
> >     public int getCounter(){ return counter; }
> >     public int getStringCounter(){ return stringCounter; }
> >     public int getIntCounter(){ return intCounter; }
> >     public int getOtherCounter(){ return otherCounter; }
> >
> >     @Override
> >     protected void act() {
> >
> >       loop(new Runnable() {
> >
> >               // Run will be executed with first message sent to actor
> >             @Override
> >           public void run() {
> >
> >               // Schedule process to retrieve second message from queue and MessagingRunnable to process it
> >               react(new MessagingRunnable<Object>(this) {
> >                     @Override
> >                   protected void doRun(final Object s) {
> >                       counter++;
> >                       System.out.println( "\nReceived in react: " + s +
> >                           " s is a " + s.getClass().getName() +
> >                           " in " + this.hashCode() + " in thread " +
> >                           Thread.currentThread().getName() );
> >                       if (s instanceof String) {
> >                           stringCounter++;
> >                           System.out.println( "s is a String: " + s );
> >                       } else if (s instanceof Integer) {
> >                           intCounter++;
> >                           System.out.println( "s is an Integer: " + s );
> >                       } else if (s instanceof Object) {
> >                           System.out.println( "s is a Object: " + s );
> >                           otherCounter++;
> >                       }
> >                       // perform if (s instanceof here)
> >
> >                   }
> >                   });
> >             } // end run
> >       });
> >     } // end act
> > } // end class
> >
> >
> >
> >
> > Would the second way still be thread-safe?
> > = Eric MacAdie
> >
> >
> >
> >
> >
> > --
> > E-mail: [hidden email]
> > Blog: http://www.jroller.com/vaclav
> > Linkedin page: http://www.linkedin.com/in/vaclavpech
> >
> >
> >
> >
> > --
> > E-mail: [hidden email]
> > Blog: http://www.jroller.com/vaclav
> > Linkedin page: http://www.linkedin.com/in/vaclavpech
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email