HEADS-project / training

Training material to get started with the HEADS technologies
10 stars 16 forks source link

Timer process sending throws exception #57

Closed sdalgard closed 9 years ago

sdalgard commented 9 years ago

It seems that the send method for some reason throws exception.....

<*** Snip from console printout **> Sender:0 sends token:5 Ender:0 got token:5 Sender:0 sends token:7 Ender:0 got token:7 Sender:0 sends token:5 Ender:0 got token:5 java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:340) at org.thingml.java.Component.send(Component.java:66) at org.thingml.generated.TimerJava.sendTimer_timeout_via_timer(TimerJava.java:44) at org.thingml.generated.TimerJava.access$000(TimerJava.java:21) at org.thingml.generated.TimerJava$1$1.run(TimerJava.java:119)

Sender:0 sends token:6 Ender:0 got token:6 java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:340) at org.thingml.java.Component.send(Component.java:66) at org.thingml.generated.TimerJava.sendTimer_timeout_via_timer(TimerJava.java:44) at org.thingml.generated.TimerJava.access$000(TimerJava.java:21) at org.thingml.generated.TimerJava$1$1.run(TimerJava.java:119)

Sender:0 sends token:8 Ender:0 got token:8 Sender:0 sends token:7 Ender:0 got token:7 Sender:0 sends token:9 Ender:0 got token:9 java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:340) at org.thingml.java.Component.send(Component.java:66) at org.thingml.generated.TimerJava.sendTimer_timeout_via_timer(TimerJava.java:44) at org.thingml.generated.TimerJava.access$000(TimerJava.java:21) at org.thingml.generated.TimerJava$1$1.run(TimerJava.java:119)

Sender:0 sends token:6 Ender:0 got token:6 Sender:0 sends token:10

<******* Snip from thingml **********>
datatype JThread
@java_type "Thread";

// Manage a set of software timers.
thing TimerJava includes Timer
@pim "Timer"
@platform "java"
{
    property timer : JThread

    statechart SoftTimer init default {
        state default {

          internal start
            event m : timer?timer_start
            guard m.delay > 0
            action do
             if (not(timer == 'null')) do
                '' & timer & '.interrupt();'
             end
             'Thread t = new Thread(){
                public void run() {
                try {
                    Thread.sleep(' & m.delay & ');'
                    timer!timer_timeout()
                '} catch (Exception ex) {
                }
                //interrupt();
                }
              };'
             timer = 't' 
              '' & timer & '.start();'                
            end

          internal cancel
            event m : timer?timer_cancel
            action do
              if (not(timer == 'null')) do
                '' & timer & '.interrupt();'
              end
            end
        }
    }
}
skorsky commented 9 years ago

The section

             try {
                Thread.sleep(' & m.delay & ');'
                timer!timer_timeout()
            '} catch (Exception ex) {
            }

is dangerous. It catches not only InterruptedException and you never know that it catched an exception. Helpful is at least to print a message which exception was catched.

brice-morin commented 9 years ago

I agree that code is not perfect. I previously used Java Timer and TimerTasks, but it was a bit heavy for realizing a simple timer. Now I just use a simple Thread, but have to deal with some lower level issues. I will try to make it better.

brice-morin commented 9 years ago

OK, I revised the timer, which should provide more useful feedback in case the internal thread is interrupted (which is normal if you cancel the timer). It should also be somehow more robust.

See https://github.com/HEADS-project/training/blob/0eddc20f8c104995579482d0d8a71c4cdc0a5576/1.ThingML_Basics/4.Timers/_java/JavaTimer.thingml

@sdalgard can you try it and close the issue if it is good enough for your case?

sdalgard commented 9 years ago

Works well for my applications :+1: