Thread pool with AT MOST one thread at a time

Hi,
I want to create a pool that creates at most one thread when it is needed. I want the pool to terminate the thread when it is idle for some time. Executors.newSingleThreadExecutor() creates a thread pool with one thread, but this thread doesn't die if idle...
The code below also doesn't work - no thread is ever created:
new ThreadPoolExecutor(0, 1, 30, TimeUnit.SECONDS,
                          new LinkedBlockingQueue<Runnable>());Thanks in advance

Use Future.get().
package net.jcip.examples;
import java.util.concurrent.*;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
* RenderWithTimeBudget
* Fetching an advertisement with a time budget
* @author Brian Goetz and Tim Peierls
public class RenderWithTimeBudget {
    private static final Ad DEFAULT_AD = new Ad();
    private static final long TIME_BUDGET = 1000;
    private static final ExecutorService exec = Executors.newCachedThreadPool();
    Page renderPageWithAd() throws InterruptedException {
        long endNanos = System.nanoTime() + TIME_BUDGET;
        Future<Ad> f = exec.submit(new FetchAdTask());
        // Render the page while waiting for the ad
        Page page = renderPageBody();
        Ad ad;
        try {
            // Only wait for the remaining time budget
            long timeLeft = endNanos - System.nanoTime();
            ad = f.get(timeLeft, NANOSECONDS);
        } catch (ExecutionException e) {
            ad = DEFAULT_AD;
        } catch (TimeoutException e) {
            ad = DEFAULT_AD;
            f.cancel(true);
        page.setAd(ad);
        return page;
    Page renderPageBody() { return new Page(); }
    static class Ad {
    static class Page {
        public void setAd(Ad ad) { }
    static class FetchAdTask implements Callable<Ad> {
        public Ad call() {
            return new Ad();
}

Similar Messages

  • When texting with more than one person at a time the incoming text scrolls to top of the conversation. anyone out there with the same issue?

    while texting with more than one person at a time the incoming texts scrolls to top of conversation. anyone having this issue?

    I've heard claims it can be done, claims some have done it, and claims that it didn't work for them. This thread is interesting, some indications it should be possible, complaints that it didn't, and also an interesting work-around:
    "How do I text message multiple people at once?"
    http://discussions.apple.com/click.jspa?searchID=-1&messageID=4822260
    You can ask Apple for iPhone changes via this link:
    http://www.apple.com/feedback/iphone.html
    Though if you're requesting hardware changes for iPhone v2 you may want to wait a week (or a month?)...they're probably going to be inundated right now by requests for software changes to the current iPhone.
    (If this has answered your question, please mark your original post as answered)

  • Is there a stereo bluetooth headset that can pair with more than one device at a time?

    Is there a stereo bluetooth headset that can pair, i.e. multipoint, with more than one device at a time?
    Are the MacBook and iPhone 4 capable of multipoint bluetooth technoloagy?
    The goal is for my wife to be able to watch her Korean TV soap operas on her MacBook and still receive a call on her iPhone 4 via a stereo bluetooth headset.
    I was looking at the Motorola S10-HD but after further review saw that it only pairs with one device at a time.
    Appreciate any and all input. My Googling has returned no results.
    Rick

    TeslasBB wrote:
    pairing my BB8330 with my blue tooth earphone(TM:jawbone) and my microsoft sync thats in my car simultaneously? if i pair with the car, will i have to pair my jawbone all over again?
    You can only pair one device at a time to your 8330, or any other phone for that matter.  The "pairings" are saved to the phone, you can use one or the other and you won't have to pair it again.  Once you turn your bluetooth device on and the phone is on, they will find each other again.
    Hope this helps,
    John
    Stevie Ray! 1954-1990
    ** Don't forget to resolve your post with the accepted solution.

  • Can I use a magic trackpad with more than one Mac at a time?

    I am attempting to pair a MTP with a second MacBook Pro.  So far I can't get system preferences to find the MTP.
    I am wondering whether the MTP can be paired with only one Mac at a time, and that this is why my MBP won't find it.
    The reason I am thinking this is because the little booklet that comes with the MTP says 'after you pair your MTP with a Mac, you can pair it again with a different Mac.  To do this, you first remove the existing pairing and then pair the trackpad again'.
    Can anyone advise, please?
    If it can be paired with more than one MBP at a time, any suggestions why my system preferences search for the MTP is not producing any results?
    Thanks

    Yes, your keyboard, mouse or trackpad can be paired with multiple Macs.  You can even have multiple keyboards, mice or trackpads paired with one Mac.  The caveat is that the device can only be connected to one Mac at a time.  The device can't be paired or connected with a Mac while it's currently connected to another.
    Pairing a device with 2 Macs will be troublesome if the Macs are located within 33 ft of each other.  The device will connect with the first available paired Mac and then unavailable to the second.   If the Macs are located outside the 10meter range, it's quite easy to power off the keyboard (forcing a disconnect) then walking the keyboard to Mac 2 and powering the keyboard on.   I do this with a keyboard from my iMac to a Mac Mini in another room.
    Captfred

  • HT1040 Is there a way to place an order with more than one item at a time? For example I am going to make a few books and have them all shipped to me.  Do I have to order and pay shipping for each one individually?

    Is there a way to place an order with more than one item at a time?  for example I am making a few books and having them all shipped to me.  Do I have to order each one separately and pay shipping each time?

    You have to order each book separately, if the books are different. Only multiple copies of the same book can be ordered in the same order, see here
    Regards
    Léonie

  • Can a user facetime with more than one user at a time

    I can use facetime with Macbook and connect to another Macbook user on another WIFI network... I was wondering if one can facetime with more than one user at a time?

    ellis911 wrote:
    I can use facetime with Macbook and connect to another Macbook user on another WIFI network... I was wondering if one can facetime with more than one user at a time?
    Only if you have more than one device running FaceTime at your end.
    Mac Pro Quad Core (Early 2009) 2.93Ghz Mac OS X (10.6.5); MacBook Pro (13 inch, Mid 2009) 2.26GHz (10.6.5)
    LED Cinema Display; G4 PowerBook 1.67GHz (10.4.11); iBookSE 366MHz (10.3.9); External iSight; iPod4touch4.2.1

  • HT5622 Can Face Time be used to connect with more than one person at a time?

    Can I connect with more than one person at a time when using Face Time?

    You mean like a conference call?  No, unfortunately.

  • How can I talk with more then one person at a time?

    How can I talk with more then one person at a time? With Faxe time? Is thier software needed to do this or can it be done with the basic package?

    You can get the drop down list by either right-clicking on the back/forward buttons, or holding down the left button until the list appears.
    If you want the drop-down arrow you can add it with the Back/forward dropmarker add-on - https://addons.mozilla.org/firefox/addon/backforward-dropmarker

  • Thread Pool with Min,Max. and other thread parameters

    hello,
    I found an Implementation of ThreadPool in the java.sun.com,but it didn't have the minimum,maximum no. of threads implemented in the code.
    Could you help me find out an implementation of Thread Pool,that keeps track of minimum no. of threads,max. no. of threads in the pool,the increment size of threads,number of idle threads,idle time allowable for a thread,etc...
    I also need to know if it is possible to have >5000 threads in a ThreadPool,beacuse I get OutofMemoryError,when I used a ThreadPool( which didn't have any of the above-mentioned parameters!)
    Thanks!!!

    Having greater then 5000 threads is a sign of a problem with your design. Threads are relativly costly
    beasties. The idea of using a pool for your threads is that you won't need to use 5000 threads
    simultaniously. Most OS's will have problems trying to create this many threads per process (unless you
    tune the OS itself (which is normally not too difficult))
    matfud

  • Creating Thread Pool with Executors.newFixedThreadPool(poolSize)

    I am creating a server socket and want a Thread pool to limit the number of threads created for serving each clients. I have written a code:
    private ExecutorService threadPool;
    //Creating a Thread Pool for handling new sessions
    threadPool=ThreadPool.createSessionThreadPool(poolSize);
    and in my main listen logic:
    serverSocket = new ServerSocket(listenPort);
    clientSocket = serverSocket.accept();
                        threadPool.execute(new Runnable(){
                             public void run(){
                                  handleConnection();
    and in handleConnection I am trying to read the inputstream in a while loop:
    public void handleConnection(){
    BufferedReader in=null;
    in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    while (true) {
    if((fullCommandString = in.readLine()) ==null){
    wait(); // I assume that this wait will block the thread and keep the thread back in to the pool.
                        System.out.println(Thread.currentThread().getName() + " received the message");
                        System.out.println("echo: " + fullCommandString);
                        out.println("230 reply back \r\n");     
    why don't it keep the thread back in the pool when I call the wait() ? Is it since the thread is still active in reading the input stream? If so can any one hepl me with a snippet which will keep the thread back in the pool when there is noting on the input stream to read.

    Hi,
    The call to wait does not return the thread pool back to the pool. The thread will be associated with your runnable as long as your run method hasn't returned.
    It looks like you instead want to use NIO if you want to use a few threads to handle many clients.
    Kaj

  • WRT 120N- Cannot connect to the internet with more than one PC at a time.

    Before I went on a three month vacation,I could connect 3 or 4 PC's either wireless or wired to my router with no problems at all.I have a desk top and a laptop computer which I use at home.After I returned,I found that I could connect to the internet with only one PC at a time.
    If I was say,connected via my laptop,my desk top would continuously keep getting disconnected and vice versa.I have been reading the suggestions from experts here and have done a few checks based on them.I also cannot get to my router page which is 192.168.1.1 I keep getting a message that says "Problem Loading Page" and this happens with both,Firefox and Internet Explorer.I am running Windows 7 - 32 bit on my desk top and Vista Home Premium on my laptop.
    I have opened the "Command Prompt" dialogue and pinged 192.168.1.1 and thereafter I typed in "ipconfig".These are the results:
    Microsoft Windows [Version 6.0.6002]
    Copyright (c) 2006 Microsoft Corporation.  All rights reserved.
    C:\Users\Gordon>ping 192.168.1.1
    Pinging 192.168.1.1 with 32 bytes of data:
    Reply from 192.168.1.1: bytes=32 time<1ms TTL=111
    Reply from 192.168.1.1: bytes=32 time<1ms TTL=111
    Reply from 192.168.1.1: bytes=32 time<1ms TTL=111
    Reply from 192.168.1.1: bytes=32 time<1ms TTL=111
    Ping statistics for 192.168.1.1:
        Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    Approximate round trip times in milli-seconds:
        Minimum = 0ms, Maximum = 0ms, Average = 0ms
    C:\Users\Gordon>ipconfig
    Windows IP Configuration
    PPP adapter Brownwolf:
       Connection-specific DNS Suffix  . :
       IPv4 Address. . . . . . . . . . . : 92.98.42.65
       Subnet Mask . . . . . . . . . . . : 255.255.255.255
       Default Gateway . . . . . . . . . : 0.0.0.0
    Ethernet adapter Local Area Connection* 22:
       Media State . . . . . . . . . . . : Media disconnected
       Connection-specific DNS Suffix  . :
    Wireless LAN adapter Wireless Network Connection:
       Connection-specific DNS Suffix  . :
       IPv6 Address. . . . . . . . . . . : 2002:5c62:2a41:c:35a2:8753:8832:f494
       Site-local IPv6 Address . . . . . : fec0::c:35a2:8753:8832:f494%2
       IPv6 Address. . . . . . . . . . . : 2002:5661:4cc9:c:35a2:8753:8832:f494
       Temporary IPv6 Address. . . . . . : 2002:5661:4cc9:c:a408:59a3:918e:f0ab
       Temporary IPv6 Address. . . . . . : 2002:5c62:2a41:c:a408:59a3:918e:f0ab
       Link-local IPv6 Address . . . . . : fe80::35a2:8753:8832:f494%12
       IPv4 Address. . . . . . . . . . . : 192.168.0.1
       Subnet Mask . . . . . . . . . . . : 255.255.255.0
       Default Gateway . . . . . . . . . :
    Ethernet adapter Local Area Connection:
       Connection-specific DNS Suffix  . :
       IPv6 Address. . . . . . . . . . . : 2002:5c62:2a41:c:413:cf4f:77af:474d
       Site-local IPv6 Address . . . . . : fec0::c:413:cf4f:77af:474d%1
       IPv6 Address. . . . . . . . . . . : 2002:5661:4cc9:c:413:cf4f:77af:474d
       Temporary IPv6 Address. . . . . . : 2002:5661:4cc9:c:1591:ad9:13da:8fea
       Temporary IPv6 Address. . . . . . : 2002:5c62:2a41:c:1591:ad9:13da:8fea
       Link-local IPv6 Address . . . . . : fe80::413:cf4f:77af:474d%11
       IPv4 Address. . . . . . . . . . . : 192.168.1.100
       Subnet Mask . . . . . . . . . . . : 255.255.255.0
       Default Gateway . . . . . . . . . : fe80::35a2:8753:8832:f494%11
                                           192.168.1.1
    Tunnel adapter Local Area Connection* 7:
       Media State . . . . . . . . . . . : Media disconnected
       Connection-specific DNS Suffix  . :
    Tunnel adapter Local Area Connection* 11:
       Media State . . . . . . . . . . . : Media disconnected
       Connection-specific DNS Suffix  . :
    Tunnel adapter Local Area Connection* 14:
       Media State . . . . . . . . . . . : Media disconnected
       Connection-specific DNS Suffix  . :
    Tunnel adapter Local Area Connection* 15:
       Media State . . . . . . . . . . . : Media disconnected
       Connection-specific DNS Suffix  . :
    Tunnel adapter Local Area Connection* 19:
       Media State . . . . . . . . . . . : Media disconnected
       Connection-specific DNS Suffix  . :
    Tunnel adapter Local Area Connection* 20:
       Connection-specific DNS Suffix  . :
       IPv6 Address. . . . . . . . . . . : 2002:5c62:2a41::5c62:2a41
       Default Gateway . . . . . . . . . : 2002:c058:6301::c058:6301
    Tunnel adapter Local Area Connection* 23:
       Media State . . . . . . . . . . . : Media disconnected
       Connection-specific DNS Suffix  . :
    Tunnel adapter Local Area Connection* 24:
       Media State . . . . . . . . . . . : Media disconnected
       Connection-specific DNS Suffix  . :
    Tunnel adapter Local Area Connection* 28:
       Media State . . . . . . . . . . . : Media disconnected
       Connection-specific DNS Suffix  . :
    I presume that this model is a modem **bleep** router as I do not have a separate modem with my system.I have a DSL connection and the firmware version of my router is ver.1.0.If I recall,I did upgrade to the next firmware upgrade a few months after I purchased the router but I am not sure.
    I use both my PC's to run a small business venture and I am worried that resetting the router may cause some other problems and will stop me from using the internet.
    The Command Prompt information above was taken from my laptop.
    Any help will be really appreciated.
    Gerard.
    Solved!
    Go to Solution.

    brownwolf66 wrote:
    No,I have a desk top and a laptop and I cannot access 192.168.1.1 on either of them.In my OP,I have used the Command Prompt dialogue on both computers to ping 192.168.1.1 and I have attached the results.This is what I got:
    Microsoft Windows [Version 6.0.6002]
    Copyright (c) 2006 Microsoft Corporation.  All rights reserved.
    C:\Users\Gordon>ping 192.168.1.1
    Pinging 192.168.1.1 with 32 bytes of data:
    Reply from 192.168.1.1: bytes=32 time<1ms TTL=111
    Reply from 192.168.1.1: bytes=32 time<1ms TTL=111
    Reply from 192.168.1.1: bytes=32 time<1ms TTL=111
    Reply from 192.168.1.1: bytes=32 time<1ms TTL=111
    Ping statistics for 192.168.1.1:
        Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    Approximate round trip times in milli-seconds:
        Minimum = 0ms, Maximum = 0ms, Average = 0ms
    The results I obtained using ipconfig in the command prompt have already been mentioned in my OP.
    I do not know if the above result is what it is supposed to be but I still cannot access my router's home page.How do I verify if there is a proxy server and if there is one,how do I disable it in my browsers?This problem occured suddenly as a few months earlier all was well.It's getting rather frustrating.
    I suggest resetting the router. Make sure firmware is updated. You can download it here - http://homesupport.cisco.com/en-us/support/routers/WRT120N. After firmware upgrade, reset and reconfigure. Just access the link below to guide you setting up the router.
    * Setting up a Linksys router for DSL Internet connection
    http://www6.nohold.net/Cisco2/ukp.aspx?vw=1&docid=20ee1457387f40178cd5f41d4b585db4_3687.xml&pid=80&r...
    * Setting up a Linksys router with Cable Internet service
    http://www6.nohold.net/Cisco2/ukp.aspx?vw=1&docid=0ff4c94586a345d082828ec2161aaecf_3686.xml&pid=80&r...

  • Can i pair my BB8330 with more than one device at a time?

    pairing my BB8330 with my blue tooth earphone(TM:jawbone) and my microsoft sync thats in my car simultaneously? if i pair with the car, will i have to pair my jawbone all over again?
    Solved!
    Go to Solution.

    TeslasBB wrote:
    pairing my BB8330 with my blue tooth earphone(TM:jawbone) and my microsoft sync thats in my car simultaneously? if i pair with the car, will i have to pair my jawbone all over again?
    You can only pair one device at a time to your 8330, or any other phone for that matter.  The "pairings" are saved to the phone, you can use one or the other and you won't have to pair it again.  Once you turn your bluetooth device on and the phone is on, they will find each other again.
    Hope this helps,
    John
    Stevie Ray! 1954-1990
    ** Don't forget to resolve your post with the accepted solution.

  • How do I update a waveform chart with multiple plots one point at a time?

    Hi,
    I am updating multiple plots one point at a time.  That is, every time I update the chart I have a value for one of the plots.  If I put in a cluster with NaN for each of the charts except the one with the data I can only get individual points on the chart.  I would rather have them connected.
    Any clues?  The only thing I can think of is to delay the update and do interpolation between samples - too complicated for me.
    Thanks

    Are the points within a given plot equally spaced in time?  If so, perhaps you need to maintain your own history of the data and used a waveform graph rewriting the history data to the graph each time.  If the points are unequally spaced, then you will need to do it as an XY graph.

  • Screenie thread per WM/DE or one thread a month?

    Following the results of this poll, it has been democratically decided that having a thread per WM/DE is better than the current method.
    However, having one screenie thread per month was also proposed and supported so this new poll seeks to find the most popular approach.
    The main problem I can see is practical - would we lock the old threads each month - or leave them open for people to post comments?  I think the latter but it is then up to posters to post new screenies in the new month thread!
    I reckon people will be dying to get there screenie top of the month tho - I ask you, what a bunch of geeks, eh? 
    The poll will run for 14 days only - if you can't get your thumb out and vote by then don't complain later!

    IceRAM wrote:If it is so beautiful, it should be "art", shouldn't it?
    Not yours    :twisted:
    I think the the threads on the forum here are going to stay for a while, i dont like the way the gallery that has been implemented works.
    Firstly theres all the crud about a 'cart' everywhere, and i can tell ya, nobody's gonna be selling screenshots.
    secondly, it's not the most usable thing, theres no "show me the last 5 screenshots uploaded" nor is there an easy or obvious way to make a comment on a particular screenshot, i eventually found the add comment link.
    the thread is good because it promotes discussion, ie, where'd you get that wallpaper or how'd you do that?
    IMHO, coppermine would have been a better choice.
    http://coppermine.sourceforge.net/
    It does a much better job, it's easier to use, doesnt have cart and printing crud everywhere, and its capable of being integrated with phpbb and phpbb's user system, allowing a single sign on.
    iphitus

  • Can you video-chat with more than one friend at a time in new Hello?

    Simply wondering if a group of people can video-chat together at same time, or just one-on-one?

    It is currently only possible to have a peer to peer connection with one other computer.
    To connect with multiple computer you would have to connect via a dedicated server and that is currently not possible with WebRTC.

Maybe you are looking for

  • Shared lib 'libskgxp8.so' is missing? WHAT THE HELL IS THAT?

    I just finished install Oracle enterprise8i, but have no database. I ran svrmgrl and sqlplus, both tools complains the same error message: oraclemaster: error while loading shared libraries: libskgxp8.so: cannot open shared object file: No such file

  • Image Rollovers w/ CSS

    I know how to make rollovers with CSS using a background image and browser text, but how would I use CSS to make rollover buttons from images (text and button are one image)? Brandon ================== Presentations Direct - Binding Machines, Laminat

  • HT203167 My Purchase history has disappeared.

    I have just tried to have a look at my purchase history and it is all but gone.  I have not changed anything like my username.  How can I get access to it again???

  • Strange Issue in PSA Table

    Hi, I have deleted all the requests in PSA, but still when I check in SE11 tcode for the same PSA table, it is showing entries. This seems strange and I faced this situation for the first time. P.S. There are no requests in PSA currently. Thanks, Kar

  • At time of switchover do we need to shutdown database

    Hi Oracle RDBMS : 11.2.0.2 RAC 4-node on RHEL 5.6 What are the steps for perfoming switchover between primary and physical standby,Do we need to shutdown the primary and physical standby databases before perfoeming switchover?