Ruby将字符串($ 100.99)转换为float或BigDecimal(Ruby convert string ($100.99) to float or BigDecimal)
我将一个页面对象“$ 100.99”作为一个字符串捕获。 有没有办法将它转换为浮点数或BigDecimal为“100.99”?
我试过
xyz.scan(/\d+/).join().to_i
但是删除了小数。I am capturing a page object “$100.99” as a string. Is there way to convert this to a float or BigDecimal as “100.99”?
I tried
xyz.scan(/\d+/).join().to_i
but that removes the decimal.
原文:https://stackoverflow.com/questions/27871906
最满意答案
使用单独的线程池是很好的默认实践,并且共享线程池是一种(可能是过早的)优化。
通过Java 7,答案是否定的,没有默认线程池,并且建议有许多线程池。 这是一种很好的分离方式,可以防止一组任务中的阻塞行为干扰另一组任务。
如果你分享线程池,你应该问问题如下:
- 日志框架能够区分任务吗? (线程是区分的一种方式。)
- 如果任务池A意外地请求过多的线程中断,应该任务池B是否饿死? 当您注意到任务池B失败时,您是否能够诊断任务池A中的问题?
- 如果A池阻止B饿死?
也许你创建了一个像
LightweightThreadpool
一样的东西。 你写的前5个任务以轻量级的方式使用它。 而第六项任务......的确如此,除了它还将错误写入磁盘,并且这些错误出人意料地大,有时还有很多错误,并且它们不会受到限制。 突然之间,前5项任务被迫挨饿,不知道是什么打击了他们,而且,当你写这些任务时,你确信他们是安全的,可能没有为这类事件做好准备。所以共享线程池就好了,因为在同一台服务器上运行两个不同的进程是可以的。 您应该首先仔细考虑资源管理,并明白现在这些任务是资源耦合的。 缺少默认线程池会试图强制默认使用单独的线程池,并在共享之前仔细考虑这些问题。
从Java 8开始,答案是“是”(根据Tagir对此问题的回答 )。 但是你会注意到,如果你向该线程池提交阻塞任务,一切都将开始可怕的失败。
Using separate threadpools is good, default practice, and sharing threadpools is a (possibly premature) optimization.
Through Java 7 the answer is no, there is not a default threadpool, and the recommendation is to have many threadpools. It's good separation and will prevent blocking behavior on one collection of tasks from interfering with another.
If you share threadpools you should ask questions like:
- will the logging framework be able to distinguish tasks? (Threads is one way to distinguish.)
- If task pool A accidentally requests way too many threads and gets cut off, should task pool B starve? When you notice task pool B is failing will you be able to diagnose the problem in task pool A?
- If pool A blocks should B starve?
Maybe you create something like a
LightweightThreadpool
. And the first 5 tasks you write use it in a lightweight fashion. And the 6th task... does, except it also writes errors to disk, and those errors are surprisingly big, and sometimes there's many of them, and they're not throttled. Suddenly the first 5 tasks are starved and have no idea what hit them, and furthermore, when you wrote those tasks, you really believed they were secure and might not have prepared for this type of incident.So sharing threadpools is about as okay as having two different processes run on the same server is okay. You should think about resource management very carefully first and understand that the tasks are resource-coupled now. The lack of a default threadpool is trying to force you to use separate ones by default, and think about these questions carefully before sharing one.
As of Java 8 the answer is "yes" (per Tagir's answer on this question). But you will notice everything will start horribly failing if you submit blocking tasks to that threadpool.
相关问答
更多-
你可以将你自己的ThreadFactory传递给ScheduledThreadPoolExecutor 。 你的ThreadFactory将创建线程,并可以给它任何你想要的名字。 您的ThreadFactory也可以重用Executors.defaultThreadFactory() ,并且只在返回线程之前更改名称。 You can pass your own ThreadFactory to ScheduledThreadPoolExecutor. Your ThreadFactory will crea ...
-
Java中每个键的线程池(Thread Pool per key in Java)[2021-12-13]
具有synchronized块的回调机制可以在这里有效地工作。 我之前在这里回答过类似的问题。 有一些限制(参见链接的答案),但它足以简单地跟踪正在发生的事情(良好的可维护性)。 我已经调整了源代码并使其更有效地适用于大多数任务将并行执行的情况(因为n和m很大),但有时必须是串行的(当任务是针对网格G的同一点时) )。 import java.util.*; import java.util.concurrent.*; import java.util.concurrent.locks.ReentrantL ... -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
Java - 重用线程池(Java - Reusing Thread Pool)[2021-11-15]
我不一定会说它本身就是劣质设计,但需要考虑一些事情。 就个人而言,我喜欢线程池,如果使用正确,它是一个强大的解决方案。 性能下降的原因是,当固定池大小为2时,一次只能运行两个线程。 有可能你需要一个更大的数字 - 可能是10,可能是100 - 但至少你可以设置它,以便你不会耗尽内存。 也就是说,如果原始设计内存不足,因为创建的线程太多,你肯定需要一次限制活动线程的数量,所以你的性能会受到一些打击...它只是不需要是全有或全无。 您还可以使用无限的执行程序线程池,假设正在运行的线程确实及时返回,这样您仍然不会 ... -
JAVA线程池重用线程(JAVA Thread pool reusing threads)[2023-10-28]
这是一种竞争条件。 如果你遵循submit()足够长的时间(在源代码中),你将到达ThreadPoolExecutor.execucte() : public void execute(Runnable command) { if (command == null) throw new NullPointerException(); /* long comment block removed */ int c = ctl.get(); if (workerCo ... -
使用单独的线程池是很好的默认实践,并且共享线程池是一种(可能是过早的)优化。 通过Java 7,答案是否定的,没有默认线程池,并且建议有许多线程池。 这是一种很好的分离方式,可以防止一组任务中的阻塞行为干扰另一组任务。 如果你分享线程池,你应该问问题如下: 日志框架能够区分任务吗? (线程是区分的一种方式。) 如果任务池A意外地请求过多的线程中断,应该任务池B是否饿死? 当您注意到任务池B失败时,您是否能够诊断任务池A中的问题? 如果A池阻止B饿死? 也许你创建了一个像LightweightThreadpo ...
-
Java Akka插件( play.libs.Akka )转发到Scala插件( play.api.libs.Akka ),后者又根据应用程序的配置启动一个新的Actor系统。 (这就是所有的插件。) 因此,您可以使用基于akka配置项的普通application.conf文件来配置ActorSystem及其所有调度程序(调度程序也是ExecutionContext)。 这些调度程序是文档所指的线程池。 导入play.api.libs.concurrent.Execution.default时使用默认线程池 ...
-
Java中的缓冲线程池?(Buffer Thread Pool in Java?)[2021-12-01]
我想你需要这样的东西: // initialization of your threads Runnable[] runnables = new Runnable[5]; for (int i=0; i<5; i++) { runnables[i] = new MyRunnable(i); } // initialization of thread pool with fixed size 3 ExecutorService executor = Executors.newFixedThreadP ... -
Java线程池同步(Java Thread Pool Synchronization)[2022-11-02]
你有没有试过看过Cyclic Barrier 。 它经过优化,允许一组线程停止并等待每个人都达到共同的障碍。 我没有看到任何理由为什么它不能与已知数量的汇集线程一起使用,并引用了公共屏障。 如果您需要在使用障碍await()计数调用的回调上进行同步,可能会有一些额外的复杂性,因为它在不同的线程中执行。 Have you tried looking at a Cyclic Barrier. It is optimized to allow a group of threads to stop and wait ... -
为什么此池不允许使用现有池中的线程子集来支持有限的并发性,同时仍然避免每次都创建新线程? Thread和ThreadFactory API不允许您回收任意Thread对象。 问题是ThreadFactory::newThread需要一个实现来返回一个给定Runnable的线程,但Thread API只允许由Thread构造函数设置Runnable 。 在Thread修复此问题会破坏模型。 (替换已启动的线程的Runnable意味着什么。) 理论上可以通过定义Thread的子类来修复,其中实际的run()方法 ...