Does Java self-limit the number of active threads?

I was hoping someone might be able to shed some light on this:
I basically have a loop which creates an instance of a class that extends Thread. This class, lets call it MyTask, may take a minute or two to run. In my run() method I system.out an ID as soon as the .start() is actioned, like this:
class MyTask extends Thread
int nMyID;
public MyTask(int vnMyID)
this.nMyID = vnMyID;
public void run()
System.out.println(this.nMyID);
// do other stuff that takes a couple of mins...
ThirdPartyAPI.dosomething();
In my loop I create an instance of MyTask and call the .start() method on it.
What I'm seeing is that rather than the ID being outputted immediately as I would expect, the IDs are being outputted over a period of time, so this to me would imply that Java is queuing the threads before starting them? I don't want it to queue the treads because the rest of MyTask is simply sat waiting for a response from a third party system. It gets through them all eventually, but it just takes a long time, and it should be quicker.
Is there anything I can do to stop it from behaving this way?
Thank you

BigBadBurrow wrote:
I was hoping someone might be able to shed some light on this:
I basically have a loop which creates an instance of a class that extends Thread. Unrelated to your problem, but just as a side note, that's poor design. You're almost certainly not creating a specialized kind of Thread, but rather, a task to be run in its own thread, and hence you should implement Runnable.
This class, lets call it MyTask, may take a minute or two to run. In my run() method I system.out an ID as soon as the .start() is actioned, like this:
class MyTask extends Thread
int nMyID;
public MyTask(int vnMyID)
this.nMyID = vnMyID;
public void run()
System.out.println(this.nMyID);
// do other stuff that takes a couple of mins...
ThirdPartyAPI.dosomething();
In my loop I create an instance of MyTask and call the .start() method on it.
What I'm seeing is that rather than the ID being outputted immediately as I would expect, the IDs are being outputted over a period of timeProvide an [url http://sscce.org]SSCCE
, so this to me would imply that Java is queuing the threads before starting them? Not generally, no. However, once you've started the other threads, you now have those threads running plus your main thread. If your main thread goes on to do some CPU intensive work, it might be some time before the other threads get cycles. Anytime you have more threads the CPUs/cores, there will be at least one thread that's waiting. You don't (and shouldn't need to) control which thread gets CPU when.

Similar Messages

  • *IMPORTANT* Limiting the number of active threads

    Hi All,
    i have a few of questions so please bare with me:
    1) What is the best way to limit the number of active threads? ie i want to run 10 threads but i want there to be only 3 running at a time.
    this is how im thinking of doing it rite now:
    count = 0;
    while(...)
    thread[count].start();
    while(threadgroup.activeCount > 4){}
    count++;
    2) I have a class that extends thread how do i overide the constructor so that i can add the thread to the threadgroup?
    ok here is an example of what i mean:
    u can do this with a normal thread: Thread one = new Thread(threadgroup, "what");
    this is what i need to do tho:
    class whatever extends Thread {
    public whatever(int a, int b, ThreadGroup tg)
    { what do i put here to make this thread part of the tg threadgroup?}
    thanx for any help in advance and i kinda need a quick answer cause this is due soon , thanx youssefe2k4

    Hi,
    1) Why do you want to do that?
    2) super(tg, "a name");
    /KajIt's part of a homework assignment... I saw another post on this forum and one at JavaRanch where you need to create an application that given a directory will scan each file and report the file name and how many lines are in them... but it must be multithreaded, each thread scan a single file and updates some list and finally you can only have three of these file scanning threads running at the same time...
    Quote "To avoid having too many threads competing for the system resources when there are a lot of files in the directory, we will restrict the number of active Producer threads (active threads are started threads that have not ended yet) during the run time. For this assignment, at any time there should not be more than 3 active Producer threads. If there are already three active Producer threads during the run time, the program should wait until one of them ends to start the next Producer thread."
    I was think about working on it, so that I can strengthen my knowledge in threads...
    I still might do it, just for fun... I don't know yet...
    We will see...
    - MaxxDmg...
    - ' He who never sleeps... '

  • Limit the number of threads

    Hello,
    I am developing an application which has to execute untrusted Java source code. The Java security manager is helpful in preventing unauthorized network and local file system access, but as far as I can tell, it can not limit the number of threads created by the untrusted application.
    How would I limit the number of threads an untrusted Java application is allowed to create?
    Jure
    Edited by: zbontarj on Feb 16, 2009 9:46 AM

    zbontarj wrote:
    Create a custom ThreadGroup will allows only a limited number of threadsI can't see any methods in ThreadGroup that limit the number of threads. There is a method that returns the number of active threads in the group, but that doesn't help me out much.ThreadGroup is not designed for easy extensibility.
    You can either take a copy and modify it to take a thread limit. (You can override the default implementation by using -Xbootclasspath/p:path or adding a jar to lib/endorsed however this may contravene the JDK license.)
    Or you can write a thread which monitors threads in the thread group every few seconds.
    Or you can use the SecurityManager to check the threads in the thread group. The SecurityManager is called each time a thread is added and it can check the threads in the current thread group.

  • BPEL Client API - Query Domain for Number of Active Threads ?

    Hi,
    Is there a method to query the number of active threads within the domain ? i.e. the same value displayed under Thread Allocation Activity shown on http://localhost/BPELConsole/default/domain.jsp?mode=thread
    I've browsed the API docs but can't find a way to do this... I might have missed something thought.
    Cheers,
    Rob

    Thanks Marc.
    How would I go about getting a domain handle ? The bit I'm stuck on in obtaining the initial authenticated connection to the server. The ngThreadInfo code does this:
    Server server = ServletUtils.getServer( request, response );
    String token = server.getServerAuth().getToken();
    and I guess the request/response vars provide the security context in which the getServerAuth operates ?
    Which API functions can I embed in a BPEL process to connect to the server and subsequently obtain the domain handle ?
    Cheers,
    Rob J.

  • This iphone activated limit the number of free accounts icloud , What to do? how to create?

       What to do? how to create?this iphone activated limit the number of free accounts icloud

    Thanks for the help!
    Unfortunately, the proposed option does not help you, as before when trying to check in icloud you see the same. Just not when you try to turn on Face Time the message "Waiting for activation" and all, Face Time icon on the desktop is not ...
    Please help!
    Thank you in advance!!

  • When you try to log into icoud message goes on this iphone activated limit the number of free accounts. What to do? How to register in icloud???

    When you try to log into icoud message goes on this iphone activated limit the number of free accounts. What to do?
    How to register in icloud???

    Thanks for the help!
    Unfortunately, the proposed option does not help you, as before when trying to check in icloud you see the same. Just not when you try to turn on Face Time the message "Waiting for activation" and all, Face Time icon on the desktop is not ...
    Please help!
    Thank you in advance!!

  • How do I limit the number of emails that show in my active mailbox?  There was a way in Settings on ios 6, but i can't find it in ios 7.

    How do I limit the number of emails that show in my active mailbox?  There was a way in Settings on ios 6, but i can't find it in ios 7.

    You need to delete your current hotmail account set up on your iphone.
    Once this has done click 'Add Account' and select the new 'outlook.com' tab.
    Set up your hotmail account again from scratch and this will then allow you to select how many weeks/months of emails you sync to your phone.
    Im not sure if anyone else had a similar problem to me prior to iOS 7, but if i deleted an email from my hotmail account on my iphone - it wouldnt sync with my actual hotmail account. So when i logged in online all the emails I had deleted on my phone would still be showing as unread.
    But by setting the account up as above, they now sync perfectly.
    Well for me anyway

  • On this iPhone is activated limit the number of free accounts

    ICLOUD On this iPhone is activated limit the number of free accounts

    Icloud is limited to 3 free icloud accounts. It is device specific and restoring will not change this. It is part of the icloud terms and conditions you agree to. In fact it says that apple reserves the limit the amount of accounts created on any one device.

  • On this iphone activated limit the number of free accounts that do?

    on this iphone activated limit the number of free accounts that do?

    Use a desktop or laptop to create the account and then
    you can use that new account with your iPhone.

  • How we can limit the number of concurrent calls to a WCF service without use the Singleton pattern or without do the change in BizTalk Configuration file?

    How can we send only one message to a WCF service at a time? How we can limit the number of concurrent calls to a WCF service without use the Singleton pattern or without do the change in BizTalk Configuration file? Can we do it by Host throttling?

    Hi Pawan,
    You need to use WCF-Custom adapter and add the ServiceThrottlingBehavior service behavior to a WCF-Custom Locations.
    ServiceThrottlingBehavior.MaxConcurrentCalls - Gets or sets a value that specifies the maximum number of messages actively processing across a ServiceHost. The MaxConcurrentCalls property specifies the maximum number of messages actively
    processing across a ServiceHost object. Each channel can have one pending message that does not count against the value of MaxConcurrentCalls until WCF begins to process it.
    Follow MSDN-
    http://msdn.microsoft.com/en-us/library/ee377035%28BTS.10%29.aspx
    http://msdn.microsoft.com/en-us/library/system.servicemodel.description.servicethrottlingbehavior.maxconcurrentcalls.aspx
    I hope this helps.
    Rachit
    If this answers your question please mark it accordingly. If this post is helpful, please vote as helpful by clicking the upward arrow mark next to my reply.

  • How do i limit the number of simultaneous downloads to my ipod touch?

    I just purchased a new ipod touch and want to dump my entire icloud library onto it.  I figured the easiest way to do this would be to make a smart playlist that includes all my music and then just download the whole playlist (thousands of songs).  The problem is, at about 2/3 of the way through a track, the ipod touch automatically starts downloading the next track. At this point, it's trying to download about 50 tracks simultaneously and as a result is taking abotu 5 minutes to download one song.  How can I limit the number of simultaneous downloads?  Is there a faster way to load up a new ipod touch with your icloud library?  Thanks.

    Thank you for the reply. I had no idea I had so much access to Firefox prefs.
    It's still a strange design though. Yep, the original setting was set to 30 simultaneous downloads. I set it to four, which would see to make my Mac more usable as four downloads are rolling while all the rest are in queue.
    Only probably is, Firefox doesn't just think of what is in the download list as a "download". Once all four downloads are rolling, no more webpages will open! (Strangely, sometimes they will, but very slowly ... but usually not at all.)
    Apparently, even a browser tab is a "download" and is stopped once the assigned number of simultaneous downloads is reached.
    So I'm still back to not being able to limit my downloads for the benefit of having good use of my browser while downloads are coming in.
    Right now, one pages where I have boo-koo downloads I want to initiate at one time, I'm clicking "download", starting the download, going to the downloads list, pausing it, getting another download, pausing it, and so on until I have a complete queue ready to go. Then I start the downloads once I know that I no longer need the internet.
    That seems sort of for the birds, doesn't it?
    Does anyone have any suggestions? Other specialized download programs get the concept of an active queue and a paused queue with a number-setting to determine the max downloads. Why doesn't Firefox get this? Or does it and have I just still not found it?

  • Limit the number of open production order

    Dear PP experts,
    I am working on discrete scenario where client want to limit the number of open production order per work center. The more elaboration is like, for one work center the only specific number of production should remain open (e.g. 5 production order in assembly shop) when user is trying to open 6th production order system should restrict that.
    Does any standard functionality available in system or needs development?
    Regards,
    Shekhar

    Dear,
    You can do it with avilability check for materail and capacity.
    In OPJK do the for your production order type as no release if materail and capacity i missing.
    Hope clear to you.
    Regards,
    R.Brahmankar

  • Report showing the number of active users in the system

    All,
    There is a standard SAP report which shows the number of active users in the system (output same as AL08).
    Does anyone remember this?
    Cheers,
    Bidwan

    Hello Bidwan, 
    Are you talking about the report RSUSR000?
    Regards.
    Ruchit.

  • How can I limit the number of emails retained on my ipad air?

    How can I limit the number of emails that are retained on my iPad Air?  Because of legal requirements of a long-term project, I cannot permanently delete emails relating to that project from my ISP's mail server.  My gmail account contains over 17,000 emails and, upon setting up my new iPad, and updating to ios 7.1.1, I now have all 17,000+ emails taking up space.  I would like to limit the emails by count as I was able to do on my old iPad running ios 5.1.1 OR to retain them based on date range.  I have not found any instructions on the support discussions or general iPad blogs that allow me to do that.  Any way around this problem?
    I also miss the 5.1.1 mail search feature that allowed me to narrow the search by from/to/subject/all.  Does anyone know if that feature is available using some little-known ios 7.1.1 feature?
    Thanks for your help.

    jlfcba wrote:
    IOS 7.1.1 doesn't show a "days to sync" parameter anywhere in SETTINGS/Mail,Contacts,Calendars/ACCOUNTS/GMAIL Account/ GMAIL Acccount Info.  There is nothing in iTunes relating to such an option either.  So, it seems I'm back to square one.
    If you have a decent mail provider like Microsoft Hotmail or Outlook.com mail it does.
    If you have Gmail it does not.  Quite right. Gmail is rubbish.

  • HT2729 I have limited space on my new ipad. How can I limit the number of songs from my itunes on my imac from going to my ipad when I connect the two?

    I have limited space on my new ipad. How can I limit the number of songs from my itunes on my imac from going to my ipad when I connect the two?

    you can do this by keeping the external hard drive connected and doing the following
    -hold shift key down on computer while opening iTunes and a prompt will appear to choose a library
    -select choose library, iTunes should bring up your explorer windows where you can search for the external HD
    -after you find the external HD find the iTunes folder you just copied to it
    -after opening the iTunes folder look for the file that says iTunes library.itl or if you can't see that extenstion just look for a document looking thing in the iTunes folder that shows up as a database file
    -double click the database file and boom iTunes should load your library without having to copy it to the computer
    PS: do make sure that if you are going to do it this way, make sure that the external HD is connected at all times, because if you don't then you will get exclamation marks next to your iTunes songs
    double PS: if you are unsure how to find out if the iTunes library file is a database file just right click it and choose properties and under the type it should say database file
    hope this helps:)

Maybe you are looking for

  • Migrar partidas em aberto do legado para o SAP em FI.

    Bom dia caros colegas, Gostaria de dividir um ponto com vocês. Temos uma nova planta sendo agregada ao SAP, e nesta planta há adiantamento a fornecedores de serviço. Sabemos que para calcular o Imposto retido é necessário lançar o  adiantamento, nest

  • Remote for Powerpoint or Keynote presentation

    Does the Apple remote work with the MacBook Air?

  • What can i do for removing SINGLE in this select query??

    What can i do for removing SINGLE in this select query given below and also NOT USE 'ENDSELECT'?? I have defined g_t_zv7_cachemapping as following :: DATA : BEGIN OF g_t_zv7_cachemapping OCCURS 0,          kostl LIKE zv7_cachemapping-kostl,         

  • Static variable behaviour in memory

    Hi, My question is : When you compile a java code, does the static variable gets the memory allocated while creating the byte code or is it on run time that a static variable gets the memory allocation and then it is shared for all instances. Thanks

  • Does Flex Builder remove unused code?

    Hi, I have alot of dead code in app that is resulting in an unnecessarily large SWF. Is there a way to make the flex compiler remove unused functions?