首页 \ 问答 \ 不可能使大小限制的缓存线程池?(Impossible to make a cached thread pool with a size limit?)

不可能使大小限制的缓存线程池?(Impossible to make a cached thread pool with a size limit?)

似乎无法使缓存的线程池对其可以创建的线程数量有限制。

以下是标准Java库中执行静态Executors.newCachedThreadPool的方式:

 public static ExecutorService newCachedThreadPool() {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                  60L, TimeUnit.SECONDS,
                                  new SynchronousQueue<Runnable>());
}

所以,使用该模板继续创建一个固定大小的缓存线程池:

new ThreadPoolExecutor(0, 3, 60L, TimeUnit.SECONDS, new SynchronusQueue<Runable>());

现在,如果你使用这个并提交3个任务,一切都会很好。 提交任何其他任务将导致被拒绝的执行异常。

试试这个:

new ThreadPoolExecutor(0, 3, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runable>());

将导致所有线程顺序执行。 即,线程池将永远不会有多个线程来处理您的任务。

这是ThreadPoolExecutor执行方法中的一个错误? 或者也许这是故意的? 还有其他一些方法?

编辑:我想要的东西完全像缓存的线程池(它根据需要创建线程,然后在一些超时后杀死它们),但是限制它可以创建的线程数,并且有能力继续排队其他任务,一旦它有达到线程限制。 根据sjlee的回应,这是不可能的。 看看ThreadPoolExecutor的execute()方法确实是不可能的。 我需要对ThreadPoolExecutor进行子类化,并覆盖execute(),有些像SwingWorker,但SwingWorker在其execute()中执行的是一个完整的hack。


It seems to be impossible to make a cached thread pool with a limit to the number of threads that it can create.

Here is how static Executors.newCachedThreadPool is implemented in the standard Java library:

 public static ExecutorService newCachedThreadPool() {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                  60L, TimeUnit.SECONDS,
                                  new SynchronousQueue<Runnable>());
}

So, using that template to go on to create a fixed sized cached thread pool:

new ThreadPoolExecutor(0, 3, 60L, TimeUnit.SECONDS, new SynchronusQueue<Runable>());

Now if you use this and submit 3 tasks, everything will be fine. Submitting any further tasks will result in rejected execution exceptions.

Trying this:

new ThreadPoolExecutor(0, 3, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runable>());

Will result in all threads executing sequentially. I.e., the thread pool will never make more than one thread to handle your tasks.

This is a bug in the execute method of ThreadPoolExecutor? Or maybe this is intentional? Or there is some other way?

Edit: I want something exactly like the cached thread pool (it creates threads on demand and then kills them after some timeout) but with a limit on the number of threads that it can create and the ability to continue to queue additional tasks once it has hit its thread limit. According to sjlee's response this is impossible. Looking at the execute() method of ThreadPoolExecutor it is indeed impossible. I would need to subclass ThreadPoolExecutor and override execute() somewhat like SwingWorker does, but what SwingWorker does in its execute() is a complete hack.


原文:https://stackoverflow.com/questions/1800317
更新时间:2023-09-06 18:09

最满意答案

你想建立自己的DE(桌面环境)。 常见的例子是GNOME和KDE,尽管其中更多的是受欢迎的。 它实际上习惯于构建自己的DE非常受欢迎,并且经常创建专用的脚本工具。

您通常需要组合多个工具:窗口管理器,工具栏程序,桌面管理器(图标),会话管理器,可能更多。 其中只有一个窗口管理器是必需的部分(通常没有任何意义,不在X服务器内运行任何WM),而其他的则是可选的。 您通常不会从头开始编写这些工具(这是很多工作),但使用已经可用的组件。

请注意,GNOME和KDE实际上都是由这些元素组成的(集成的)。 例如,GNOME包括一个窗口管理器( metacity ),工具栏( gnome-panel ),桌面管理器( nautilus )等等。 如果需要,您可以将一个元素更改为另一个元素:它曾经是流行的使用sawfish作为窗口管理器而不是metacity ,保持所有其他元素完整。 现在使用compiz而不是metacity是受欢迎的。

我曾经使用FVWM ,它是一个窗口管理器,具有额外的(相当不错的)脚本功能,可用于创建工具栏,菜单和其他内容。 一个很好的例子是FVWM-Crystal ,它是一个基于FVWM的完整的DE,还有一些其他工具,它们是以FVWM脚本和Python的混合编写的。 您只需打开FVWM的脚本控制台并输入命令即可在运行时改变任何行为。 如果要从已经可用的东西开始,FVWM-Crystal可以是一个很好的起点; 如果你想从头开始构建你的DE,那么赤裸裸的FVWM是好的。

[更新:看起来, gnome-shell在这方面与FVWM非常相似。 它的大部分行为是使用JavaScript编写的(而不是FVWM中的自定义语言),并且非常容易改变。 它也是非常积极的开发,因为它是GNOME3的重要组成部分,非常支持组合。 这是一个黑客的天堂;-)]

[UPDATE2:所以好像我是在gnome-shell上。 有一个肉桂 ,它使用与gnome-shell相同的框架( mutter窗口管理器),但是构建一个以不同于gnome-shell方式工作的桌面。]

有一个网站是一个很好的思路来源: lynucs.org 。 现在已经下了两年了,但已经存档了 。

警告:许多这些组件不再被维护,特别是因为GNOME / KDE开始变得实际可用。 这方面的发展通常由嵌入式设备的特定要求驱动: 手机或上网本 。

现在,回答你的实际问题:

  • Linux / OS flaver:任何容易定制的:Debian,Arch,Gentoo, LFS ...
  • 工具/框架:这取决于你想要实现什么。 如果要编写自定义工具栏,您可能只需使用一些脚本语言(如FVWM),自定义工具(如adesklet) ,或者在Qt中编写自己的工具(如果大多数其他组件都在Qt中)或GTK(如果大多数其他组件在GTK中)。
  • 个人用户界面:其中有很多,像lynucs.org这样的网站是一个很好的汇编(每个桌面使用的lynucs列出的组件)。
  • 复杂的透明度效果:较旧的工具通常不会原生处理,或者做一些简单的事情,只是将部分桌面壁纸重新使用为自己的背景(所谓的假透明度)。 很可能你需要编写自己的代码,fe作为compiz的插件。

You want to build your own DE (desktop environment). Common examples are GNOME and KDE, although more of them were popular. It actually used to be very popular to build your own DE, and dedicated scripting tools were often created.

You will usually need to combine multiple tools: a window manager, a toolbar program, a desktop manager (icons), session manager, possibly more. Out of that only a window manager is a required part (it usually doesn't make any sense not to run any WM inside X server), and others are optional. You will usually not write these tools from scratch (it is a lot of work), but use already available components.

Note that both GNOME and KDE actually consists of these elements (well integrated). For example GNOME consists of a window manager (metacity), toolbars (gnome-panel), desktop manager (nautilus) and so on. You can change one element into another if you want: it used to be popular to use sawfish as a window manager instead of metacity, keeping all the other elements intact. Now it is popular to use compiz instead of metacity.

I used to use FVWM, which is a window manager with an additional (quite good) scripting capabilities that could be used to create toolbars, menus and other things. One of nice examples is FVWM-Crystal, which is a complete DE built on FVWM and few other tools, written in a mix of FVWM scripting and Python. You could actually change any kind of behaviour at runtime by just opening FVWM's scripting console and typing commands. FVWM-Crystal can be a good starting point if you want to start from something already usable; bare FVWM is good if you want to build your DE from scratch.

[UPDATE: it seems that gnome-shell is very similar in this regard to FVWM. Most of its behavior is scripted using JavaScript (instead of a custom language in FVWM), and is very easy to change. It is also very actively developed, as it is an important part of GNOME3, with very good support for composition. It's a hacker's heaven ;-)]

[UPDATE2: so it seems I was right on gnome-shell. There's a Cinnamon, which uses the same framework (mutter window manager) as gnome-shell, yet builds a desktop which works in a different way than gnome-shell.]

There was a website that was a good source of ideas to implement: lynucs.org. It is down for two years now, but it got archived.

Warning: lots of these components are not maintained any more, especially since GNOME/KDE started to become actually usable. Development in this area is usually driven more by specific requirements of embedded devices: phones or netbooks.

Now, to answer your actual questions:

  • Linux/OS flaver: anything easily customizable: Debian, Arch, Gentoo, LFS...
  • Tools/frameworks: it depends on what do you want to achieve. If you want to write a custom toolbar you might just use some scripting languages (like FVWM's), custom tools like adesklets, or write your own in Qt (if most of your other components is in Qt) or GTK (if most of your other components is in GTK).
  • Personal UIs: there were lots of them, and sites like lynucs.org was a nice compilation of them (lynucs listed components used in each desktop).
  • Complex transparency effects: older tools usually do not handle it natively, or do a simple things like just reusing part of desktop wallpaper as its own background (so-called fake transparency). Most probably you'll need to write your own code, f.e. as a plugin to compiz.

相关问答

更多
  • linux怎样进入GUI[2023-09-20]

    你使用redlinux(红旗Linux)等就是Gui界面,现在有很多GUI方式的Linux系统的。
  • 都可以。c++相关的话, 要用Qt或者gtk或者wxWidgets之类的。而且python之类的,qt有对应的port。如果是java那就不用说了吧。c#的话,用mono,就是所谓的gtk#.
  • 那就命令行安装咯 像Freebsd gentoo不就那么装的么 官网都有说明的 装起来比较烦
  • 我发现自己主要使用gitg与命令行结合使用更复杂的任务。 源库: http : //git.gnome.org/browse/gitg 。 最近,已经发布了一个新版本0.3.2,这是使用Vala的重写。 与旧的0.2系列相比,它还没有功能完整,但是重构器有望加快未来的发展。 此外,Launchpad上通常有PPA,其中包含比标准回购更新的版本。 I find myself using mainly gitg in combination with the command line for more comp ...
  • 我可以强烈推荐Squirrel SQL 。 也看到这个相似的问题: 开发工具直接访问数据库 I'm sticking with DbVisualizer Free until something better comes along. EDIT/UPDATE: been using https://dbeaver.io/ lately, really enjoying this
  • 你想建立自己的DE(桌面环境)。 常见的例子是GNOME和KDE,尽管其中更多的是受欢迎的。 它实际上习惯于构建自己的DE非常受欢迎,并且经常创建专用的脚本工具。 您通常需要组合多个工具:窗口管理器,工具栏程序,桌面管理器(图标),会话管理器,可能更多。 其中只有一个窗口管理器是必需的部分(通常没有任何意义,不在X服务器内运行任何WM),而其他的则是可选的。 您通常不会从头开始编写这些工具(这是很多工作),但使用已经可用的组件。 请注意,GNOME和KDE实际上都是由这些元素组成的(集成的)。 例如,GNO ...
  • 我想你在这里混合术语。 GUI开发可以使用几个库在C ++中完成,包括QT和wxWidgets。 http://qt.nokia.com/ http://www.wxwidgets.org/ Windows上的GUI可以使用其他几个库创建,包括Win32 API,MFC,WinForms(.NET),Windows Presentation Foundation(WPF。,.NET)。 等等 I think you are mixing terms here. GUI development can be ...
  • 我看到了推动wx和gtk的答案,所以我无法避免推动Qt ,我最喜欢的!) - 一个主要的公司站在它后面(诺基亚), 两套出色的Python绑定(PyQt和PySide),支持Python 3,精湛的Qt Designer ,出色的Mac和移动支持......这真的很难打败......!) I see answers pushing wx and gtk, so I can't avoid pushing Qt, my favorite!-) With a major corporation standing ...
  • 它通常很大程度上取决于您关注的DE (桌面环境); 每个人通常都有一个使用最多的GUI工具包。 例如, GNOME主要使用GTK + ; 由于GTK +是用C语言编写的,许多Linux GTK + GUI应用程序都使用它,但也有其他语言的绑定,比如Python,我也经常看到它。 并且还有其他语言的绑定。 另一方面,你有KDE ,它主要基于Qt ; 而且,由于Qt是用C ++编写的,因此大多数使用Qt的应用程序也是用C ++编写的。 但是,说实话,语言的选择并不重要; 它更重要的是工具包。 我会选择您使用的那 ...
  • 我建议gtkmm 。 它具有现代,便携和面向对象的特点。 并且它不使用奇怪的预编译器:) I suggest gtkmm. It's modern, portable and object oriented. And it doesn't use a weird pre-compiler :)

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。