litesoft
@ 947
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.delayed.nonpublic; import org.litesoft.commonfoundation.base.*; import org.litesoft.core.delayed.nonpublic.*; import org.litesoft.core.util.*; public class ThreadBasedDelayedTimedRunnableRunner extends AbstractDelayedTimedRunnableRunner { private final MyRunnable mMyRunnable; public ThreadBasedDelayedTimedRunnableRunner( MillisecTimeSource pMillisecTimeSource ) { super( pMillisecTimeSource, LoggingUnexpectedExceptionHandler.INSTANCE ); mMyRunnable = new MyRunnable(); } public ThreadBasedDelayedTimedRunnableRunner() { this( null ); } @Override protected void LLscheduleFor( long pWhenTimeInMilliSecs, long pNowInMilliSecs ) { if ( pWhenTimeInMilliSecs <= 0 ) { throw new IllegalArgumentException( "WhenTimeInMilliSecs is not greater than 0 with (" + // mTimedRunnable.getClass() + "): " + pWhenTimeInMilliSecs ); } mMyRunnable.scheduleFor( pWhenTimeInMilliSecs ); } @Override protected void LLcancel() { mMyRunnable.cancel(); } public boolean isScheduled() { return mMyRunnable.isScheduled(); } private class MyRunnable implements Runnable { private Thread mMyThread = null; private Long mTillTime = null; public synchronized boolean isScheduled() { return (mTillTime != null); } public synchronized void scheduleFor( long pWhenTimeInMilliSecs ) { mTillTime = pWhenTimeInMilliSecs; if ( mMyThread != null ) { notifyAll(); } else { mMyThread = new Thread( this ); mMyThread.setDaemon( true ); mMyThread.start(); } } @Override public void run() { while ( waitTillTime() ) { LLrun(); } } private synchronized boolean waitTillTime() { while ( mMyThread != null ) { try { while ( mTillTime == null ) { wait(); } long zNow = mMillisecTimeSource.now(); if ( mTillTime <= zNow ) { mTillTime = null; return true; } long milliSecsToWait = mTillTime - zNow; wait( milliSecsToWait ); } catch ( InterruptedException e ) { // Whatever... } } return false; } public synchronized void cancel() { mTillTime = null; if ( mMyThread != null ) { notifyAll(); } } } } |
Commits for litesoft/trunk/Java/core/Server/src/org/litesoft/delayed/nonpublic/ThreadBasedDelayedTimedRunnableRunner.java
Revision | Author | Commited | Message |
---|---|---|---|
947 Diff | GeorgeS | Fri 06 Jun, 2014 23:36:56 +0000 | Correct Spelling of package! |
942 Diff | GeorgeS | Mon 02 Jun, 2014 23:41:46 +0000 | Extracting commonfoundation |
858 Diff | GeorgeS | Sun 04 Nov, 2012 18:40:40 +0000 | |
49 Diff | GeorgeS | Mon 12 Apr, 2010 02:59:10 +0000 | License Text |
24 Diff | GeorgeS | Wed 24 Feb, 2010 01:51:38 +0000 | |
2 | GeorgeS | Sun 07 Feb, 2010 12:50:58 +0000 |