本文整理汇总了Java中java.util.TimerTask类的典型用法代码示例。如果您正苦于以下问题:Java TimerTask类的具体用法?Java TimerTask怎么用?Java TimerTask使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TimerTask类属于java.util包,在下文中一共展示了TimerTask类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: startItem
import java.util.TimerTask; //导入依赖的package包/类
@Override
protected void startItem(PlayableItem playableItem, Object loadedItem) {
// Communicating with the activity is only possible after the activity
// is started
// if we send an broadcast event to early the activity won't be up
// because there is no known way to query the activity state
// we are sending the command delayed
DIDLObject.Property<URI> albumArtUriProperty = playableItem.getItem() == null ? null : playableItem.getItem().getFirstProperty(DIDLObject.Property.UPNP.ALBUM_ART_URI.class);
albumArtUri = (albumArtUriProperty == null) ? null : albumArtUriProperty.getValue();
commandExecutionTimer = new Timer();
commandExecutionTimer.schedule(new TimerTask() {
@Override
public void run() {
Intent intent = new Intent();
intent.setAction(BackgroundMusicBroadcastReceiver.ACTION_PLAY);
getContext().sendBroadcast(intent);
}
}, 600L);
}
示例2: timerOne
import java.util.TimerTask; //导入依赖的package包/类
public void timerOne()
{
timer.schedule(new TimerTask()
{
@Override
public void run()
{
System.out.println("timerOne invoked ,the time:"
+ (System.currentTimeMillis() - start));
try
{
Thread.sleep(4000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}, 1000);
}
示例3: SpongeStats
import java.util.TimerTask; //导入依赖的package包/类
public SpongeStats(MCMarketApi marketApi, PluginContainer plugin) {
super(marketApi);
Sponge.getEventManager().registerListeners(plugin, this);
Timer timer = new Timer(true);
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (!Sponge.getPluginManager().isLoaded(plugin.getId())) {
timer.cancel();
return;
}
Sponge.getScheduler().createTaskBuilder().execute(() -> runEventsSender()).submit(plugin);
}
}, 1000 * 10, 1000 * 60);
}
示例4: put
import java.util.TimerTask; //导入依赖的package包/类
public void put(MyElement<K, V> element) {
if (elements.size() == maxElements) {
K firstKey = elements.keySet().iterator().next();
elements.remove(firstKey);
}
K key = element.getKey();
elements.put(key, element);
if (!isEternal) {
if (lifeTimeMs != 0) {
TimerTask lifeTimerTask = getTimerTask(key, lifeElement -> lifeElement.getCreationTime() + lifeTimeMs);
timer.schedule(lifeTimerTask, lifeTimeMs);
}
if (idleTimeMs != 0) {
TimerTask idleTimerTask = getTimerTask(key, idleElement -> idleElement.getLastAccessTime() + idleTimeMs);
timer.schedule(idleTimerTask, idleTimeMs, idleTimeMs);
}
}
}
示例5: submit
import java.util.TimerTask; //导入依赖的package包/类
public synchronized void submit(final RunnableFuture task, long delay)
{
getTimer().schedule(new TimerTask()
{
@Override
public void run()
{
Thread t = new Thread(new Runnable()
{
@Override
public void run()
{
submit(task);
}
});
t.setDaemon(daemon);
t.start();
}
}, delay);
}
示例6: startSubmitting
import java.util.TimerTask; //导入依赖的package包/类
/**
* Starts the Scheduler which submits our data every 30 minutes.
*/
private void startSubmitting() {
final Timer timer = new Timer(true); // We use a timer cause the Bukkit scheduler is affected by server lags
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (!plugin.isEnabled()) { // Plugin was disabled
timer.cancel();
return;
}
// Nevertheless we want our code to run in the Bukkit main thread, so we have to use the Bukkit scheduler
// Don't be afraid! The connection to the bStats server is still async, only the stats collection is sync ;)
Bukkit.getScheduler().runTask(plugin, new Runnable() {
@Override
public void run() {
submitData();
}
});
}
}, 1000*60*5, 1000*60*30);
// Submit the data every 30 minutes, first time after 5 minutes to give other plugins enough time to start
// WARNING: Changing the frequency has no effect but your plugin WILL be blocked/deleted!
// WARNING: Just don't do it!
}
示例7: sendTimeUpdates
import java.util.TimerTask; //导入依赖的package包/类
/**
* Start sending time events to listeners.
* @param playerIndex Player index to send times for
*/
private void sendTimeUpdates(int playerIndex) {
this.timeUpdateSender = new TimerTask() {
long startTime = System.currentTimeMillis();
long moveTime = settings.getMoveTimeMillis();
long gameTime = times[playerIndex - 1];
@Override
public void run() {
long elapsed = System.currentTimeMillis() - startTime;
gameTime -= elapsed;
moveTime -= elapsed;
// Broadcast the elapsed times since the last TimerTask
if(settings.gameTimingEnabled()) {
listeners.forEach(listener -> listener.gameTimeChanged
(playerIndex, gameTime));
}
if(settings.moveTimingEnabled()) {
listeners.forEach(listener -> listener.moveTimeChanged
(playerIndex, moveTime));
}
startTime = System.currentTimeMillis();
}
};
timer.scheduleAtFixedRate(timeUpdateSender, 0, 100);
}
示例8: main
import java.util.TimerTask; //导入依赖的package包/类
public static void main(String []s)
{
final Dialog fd = new Dialog(new Frame(), true);
Timer t = new Timer();
t.schedule(new TimerTask() {
public void run() {
System.out.println("RUNNING TASK");
fd.setVisible(false);
fd.dispose();
System.out.println("FINISHING TASK");
}
}, 3000L);
fd.setVisible(true);
t.cancel();
Util.waitForIdle(null);
AbstractTest.pass();
}
示例9: runQuery
import java.util.TimerTask; //导入依赖的package包/类
public static Thread runQuery(final String query,
final Consumer<Boolean> success,
final Consumer<BackendException> failure,
final long timeout) {
final Consumer<Engine> engineConsumer = engine -> {
if(timeout >= 0) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
synchronized (engineLock) {
if(engine == null) return;
engine.cancel();
}
}
}, timeout);
}
};
return runQuery(query, success, failure, engineConsumer);
}
示例10: startMp4
import java.util.TimerTask; //导入依赖的package包/类
public void startMp4(File f) {
mVideoFragment = VideoFragment.newInstance(f.getPath(), true);
transaction.add(R.id.init_frame, mVideoFragment, "initVideo").commit();
exitTimer = new Timer();
exitTimer.schedule(new TimerTask() {
@Override
public void run() {
videoTime--;
InitStartActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (exitInit != null) {
exitInit.setText(mWeApplication.getResources().getString(R.string.exit_init) + videoTime);
if (videoTime <= 0) {
HomeActivity.show(InitStartActivity.this);
InitStartActivity.this.finish();
}
}
}
});
}
}, 0, 1 * 1000);
}
示例11: fadeOut
import java.util.TimerTask; //导入依赖的package包/类
private void fadeOut() {
cancelFadeTasks();
if (musicPlayer != null && volume > 0.1f) {
fadeOutTask = new TimerTask() {
@Override
public void run() {
if (musicPlayer != null) {
volume -= 0.1f;
if (volume < 0.1f) {
cancel();
}
try {
musicPlayer.setVolume(volume, volume);
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
}
};
timer.schedule(fadeOutTask, 0, 100);
}
}
示例12: fadeIn
import java.util.TimerTask; //导入依赖的package包/类
private void fadeIn() {
cancelFadeTasks();
if (musicPlayer != null && volume < 0.9f) {
fadeInTask = new TimerTask() {
@Override
public void run() {
if (musicPlayer != null) {
volume += 0.1f;
if (volume > 0.9f) {
cancel();
}
try {
musicPlayer.setVolume(volume, volume);
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
}
};
timer.schedule(fadeInTask, 0, 100);
}
}
示例13: onFinish
import java.util.TimerTask; //导入依赖的package包/类
@Override
public void onFinish() {
// 如果计时器正常结束,则开始计步
time.cancel();
StepSensorBase.CURRENT_STEP += TEMP_STEP;
lastStep = -1;
Log.v(TAG, "计时正常结束");
timer = new Timer(true);
TimerTask task = new TimerTask() {
public void run() {
if (lastStep == StepSensorBase.CURRENT_STEP) {
timer.cancel();
CountTimeState = 0;
lastStep = -1;
TEMP_STEP = 0;
Log.v(TAG, "停止计步:" + StepSensorBase.CURRENT_STEP);
} else {
lastStep = StepSensorBase.CURRENT_STEP;
}
}
};
timer.schedule(task, 0, 2000);
CountTimeState = 2;
}
示例14: Batcher
import java.util.TimerTask; //导入依赖的package包/类
public Batcher(BatchedAction<T> action, long interval, final Handler handler){
this.interval = interval;
if (handler != null)
this.handler = new WeakReference<Handler>(handler);
timerTask = new TimerTask() {
@Override
public void run() {
if (pulled && !toHold())
{
trigger();
}
}
};
timer.scheduleAtFixedRate(timerTask, this.interval, this.interval);
setBatchedAction(action);
}
示例15: stopItem
import java.util.TimerTask; //导入依赖的package包/类
@Override
protected void stopItem(PlayableItem playableItem) {
// Communicating with the activity is only possible after the activity
// is started
// if we send an broadcast event to early the activity won't be up
// because there is no known way to query the activity state
// we are sending the command delayed
commandExecutionTimer = new Timer();
commandExecutionTimer.schedule(new TimerTask() {
@Override
public void run() {
Intent intent = new Intent();
intent.setAction(BackgroundMusicBroadcastReceiver.ACTION_STOP);
getContext().sendBroadcast(intent);
}
}, 600L);
}