diff options
| -rw-r--r-- | src/com/isode/stroke/network/JavaTimer.java | 47 |
1 files changed, 25 insertions, 22 deletions
diff --git a/src/com/isode/stroke/network/JavaTimer.java b/src/com/isode/stroke/network/JavaTimer.java index ac9e219..2baae3d 100644 --- a/src/com/isode/stroke/network/JavaTimer.java +++ b/src/com/isode/stroke/network/JavaTimer.java | |||
| @@ -4,21 +4,20 @@ | |||
| 4 | * See Documentation/Licenses/GPLv3.txt for more information. | 4 | * See Documentation/Licenses/GPLv3.txt for more information. |
| 5 | */ | 5 | */ |
| 6 | /* | 6 | /* |
| 7 | * Copyright (c) 2010, Isode Limited, London, England. | 7 | * Copyright (c) 2010-2018, Isode Limited, London, England. |
| 8 | * All rights reserved. | 8 | * All rights reserved. |
| 9 | */ | 9 | */ |
| 10 | package com.isode.stroke.network; | 10 | package com.isode.stroke.network; |
| 11 | 11 | ||
| 12 | import java.util.Date; | ||
| 13 | |||
| 14 | import com.isode.stroke.eventloop.Event; | 12 | import com.isode.stroke.eventloop.Event; |
| 15 | import com.isode.stroke.eventloop.EventLoop; | 13 | import com.isode.stroke.eventloop.EventLoop; |
| 14 | import com.isode.stroke.eventloop.EventOwner; | ||
| 16 | 15 | ||
| 17 | class JavaTimer extends Timer { | 16 | class JavaTimer extends Timer { |
| 18 | 17 | ||
| 19 | private class TimerRunnable implements Runnable { | 18 | private class TimerRunnable implements Runnable,EventOwner { |
| 20 | 19 | ||
| 21 | boolean running_ = true; | 20 | private boolean running_ = true; |
| 22 | private final EventLoop eventLoop_; | 21 | private final EventLoop eventLoop_; |
| 23 | private final long milliseconds_; | 22 | private final long milliseconds_; |
| 24 | 23 | ||
| @@ -28,25 +27,27 @@ class JavaTimer extends Timer { | |||
| 28 | } | 27 | } |
| 29 | 28 | ||
| 30 | public void run() { | 29 | public void run() { |
| 31 | long endTime = new Date().getTime() + milliseconds_; | 30 | long endTime = System.currentTimeMillis() + milliseconds_; |
| 32 | while (shouldEmit() && new Date().getTime() < endTime) { | 31 | while (shouldEmit() && System.currentTimeMillis() < endTime) { |
| 33 | try { | 32 | try { |
| 34 | long timeToWait = endTime - new Date().getTime(); | 33 | long timeToWait = endTime - System.currentTimeMillis(); |
| 35 | if (timeToWait > 0) { | 34 | if (timeToWait > 0) { |
| 36 | Thread.sleep(milliseconds_); | 35 | Thread.sleep(timeToWait); |
| 37 | } | 36 | } |
| 38 | } catch (InterruptedException ex) { | 37 | } catch (InterruptedException ex) { |
| 39 | // Needs to be caught, but won't break out of the loop | 38 | // Needs to be caught, but won't break out of the loop |
| 40 | // unless end time reached or stop() has been called. | 39 | // unless end time reached or stop() has been called. |
| 41 | } | 40 | } |
| 42 | } | 41 | } |
| 43 | if (shouldEmit()) { | 42 | synchronized(this) { |
| 44 | eventLoop_.postEvent(new Event.Callback() { | 43 | if (shouldEmit()) { |
| 45 | public void run() { | 44 | eventLoop_.postEvent(new Event.Callback() { |
| 46 | onTick.emit(); | 45 | public void run() { |
| 47 | } | 46 | onTick.emit(); |
| 48 | }); | 47 | } |
| 49 | } | 48 | },this); |
| 49 | } | ||
| 50 | } | ||
| 50 | } | 51 | } |
| 51 | 52 | ||
| 52 | 53 | ||
| @@ -56,6 +57,7 @@ class JavaTimer extends Timer { | |||
| 56 | 57 | ||
| 57 | public synchronized void stop() { | 58 | public synchronized void stop() { |
| 58 | running_ = false; | 59 | running_ = false; |
| 60 | timer_.eventLoop_.removeEventsFromOwner(this); | ||
| 59 | } | 61 | } |
| 60 | } | 62 | } |
| 61 | 63 | ||
| @@ -85,14 +87,15 @@ class JavaTimer extends Timer { | |||
| 85 | @Override | 87 | @Override |
| 86 | public void stop() { | 88 | public void stop() { |
| 87 | timer_.stop(); | 89 | timer_.stop(); |
| 88 | //FIXME: This needs to clear any remaining events out of the EventLoop queue. | ||
| 89 | } | 90 | } |
| 90 | 91 | ||
| 91 | @Override | 92 | @Override |
| 92 | public String toString() { | 93 | public String toString() { |
| 93 | return "JavaTimer for " + timer_.milliseconds_ + | 94 | synchronized (timer_) { |
| 94 | " milliseconds " + | 95 | return "JavaTimer for " + timer_.milliseconds_ + |
| 95 | (timer_.running_ ? "running" : "not running"); | 96 | " milliseconds " + |
| 97 | (timer_.running_ ? "running" : "not running"); | ||
| 98 | } | ||
| 96 | } | 99 | } |
| 97 | private final TimerRunnable timer_; | 100 | private final TimerRunnable timer_; |
| 98 | } | 101 | } |
Swift