Scala中的后台任务

编程入门 行业动态 更新时间:2024-10-24 22:31:10
本文介绍了Scala中的后台任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个要定期检查和修剪的缓存.在Java中,我将执行以下操作:

I have a cache I want to periodically check and prune. In Java, I'd do the following:

new Thread(new Runnable() { void run() { while (true) { Thread.sleep(1000); // clear the cache's old entries } } }).start();

当然,我将线程安全类型用作高速缓存时会遇到一些问题,但撇开这个问题,我的问题很简单. Scala运行重复后台任务的方式是什么-您不想在应用程序的主线程中运行某些东西?

Sure, I'd have some issues with thread-safe types to use as the cache, but putting that aside, my question is simple. What's the Scala way of running a recurring background task -- something you don't want running in the application's main thread?

我已经使用了一些actor,我想在这种情况下我的问题是我没有任何东西可以生成一条消息,表明该是时候清除缓存了.或者更确切地说,我能想到的生成这些消息的唯一方法是创建一个线程来执行此操作...

I've used actors a bit and I guess my problem in this scenario is that I don't have anything to generate a message that it's time to clear the cache. Or rather, the only way I can imagine to generate those messages is to create a thread to do it...

我需要人们对答案进行投票-他们对我来说都很好

推荐答案

有很多方法可以做到这一点,但我会做一些简单的事情,如下所示.

There are many ways to do that, but I would do something simple like the following.

import scala.concurrent.ops._ spawn { while (true) { Thread.sleep(1000); // clear the cache's old entries } }

希望这会有所帮助.

更多推荐

Scala中的后台任务

本文发布于:2023-11-15 05:48:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1591697.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后台   Scala

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!