Blocked threads

Hi, I need help to solve the following problem:
I have a thread T calling a method x() of a class C.
The problem is that, by my point of view, the method C.x() is an atomic function and it could be blocking or it could take too much time.
Now I need to kill the thread after a while (i.e. after timeout expiration).
I know that the Thread.close() method is deprecated and that, in this case, Thread.interrupt() can't solve the problem (I can't use status variables).
I've also read something about java.nio package but I don't know how to use it.
Is there anybody can help me?
Thanks

I'm implementing a simple server that gets request from clients.
Clients open a socket connection on a well known port.
When the server receives a new connection request it creates a new thread.
This thread is in charge to receive, parse the commands coming from the client (via socket). Based on the request it calls the related method for the class. Normally the method is a call for a CORBA object's function. And the problem is that the response (that is, the function has finished) could come after a while and it is out of my control (for example CORBA objects has problems or the request itself takes time).
After timeout expiration I need to kill all the blocked threads, because the server must handle a maximum number of threads, and if I don't kill blocked threads no other clients (requests) are admitted.
thanks for your help
d.

Similar Messages

  • Stopping a blocked Thread

    I need to stop a Thread, or interrupt a blocked I/O.
    I'm making a call to a library that I do not own and cannot change. The library makes a HTTP connection to a service and waits for a response. As far as I know, the library uses BufferedReader to read the service response. If the service is down, then no response is ever returned, and the BufferedReader read simply blocks.
    I cannot change the library to be intelligent, of course, and don't have very good luck getting the service support to fix their stuff. But I have to use the library. How can I interrupt the blocked I/O read?
    What I've tried is as follows, but I'm not convinced it's the right way. My application is a long-running application that does not exit very often, so I'm concerned also about gc not properly recycling the resources due to still having the blocked thread still there (and this has impacted subsequent executions, even though I use local variables where I can).
            // Create a new Thread to wait on the submission
            Thread t = new Thread() {
                public void run() {
                    response = event.submit();
            t.setDaemon(true);
            t.start();
            try {
                t.join(timeOut);
                msg = "Attempt to connect to ESM Server timed out";
            catch (InterruptedException e) {
                msg = e.getMessage();
            }The other option I was thinking of, was coding my call to ESM server in a separate Class with a main, and invoking it via Runtime.exec. That way, at least I'm 100% sure that the code exits, and I can use System.out/err to receive the "output".
    Given that Thread.stop() is deprecated, and that you theoretically cannot interrupt blocked I/O, how have others solved this problem?
    Thanks,
    Matt

    ...you theoretically cannot interrupt blocked I/O, how have others solved this problem?I've done it your way. Each thread does an access. If it takes too long, then it may never respond and I ignore it. I also log the error condition and try to notify and administrator.
    Threads probably take up about a megabyte of memory so I may run out of memory it the problem doesn't get fixed by manual intervention.
    Your idea of using Runtime means that you create a new JVM for each ESM. If a thread takes up a megabyte, the new JVM takes up many, many megabytes. Instead of running out of memory in the JVM, you may run out of memory in the Box.
    Every situation is different. My thread approach works for my application. What works best for you is your call.

  • Obtaining monitor lock of a blocked thread

    Is there a way to obtain the monitor lock (reference to the object) of a blocked thread? Something like Object Thread.getMonitorLockedObject()Also when you get the locked object it would be useful if you can get reference to the thread which is holding it, e.g.
    Thread Object.getLockingThread() although the latter could be achieved through iterating over all threads and calling boolean Thread.holdsLock(Object)Such methods would help very much in debugging deadlocks and other concurrency problems.

    <pre>
    Is there a way to obtain the monitor lock (reference
    to the object) of a blocked thread? Something like
    Object Thread.getMonitorLockedObject()
    Not that I'm aware of, and a quick glance through the APi didn't look promising.
    Also when you get the locked object it would be
    useful if you can get reference to the thread which
    is holding it, e.g.
    Thread Object.getLockingThread()although the latter could be achieved through
    iterating over all threads and calling boolean
    Thread.holdsLock(Object)
    Again, no. And your work-around wouldn't work, since the holdsLock method is static and can only be called for the current thread.
    Such methods would help very much in debugging
    deadlocks and other concurrency problems.There already are some useful methods for that in the Thread class, e.g. getState() and getAllStackTraces().
    </pre>

  • How to see and clear the blocked threads

    Where can I see how many threads are currently running and how many threads are blocked and how many are in idle status.
    Where can I see all these information and if I want to kill the blocked threads how to do that
    Thanks in advance

    This can be found in Weblogic Log folder..

  • CPU going 100%. Can blocked thread may be a reason?

    Hi Team,
    May be a dumb question. In my application around 40 threads are in blocked state and CPU is going about 100%. Is blocked threads may be reason for that? As threads are blocked, CPU should not be busy entertaining them, that is the reason why this question came in mind. Can somebody clear my this doubt.
    Thanks in advance.

    I encounter the same problem, although I run Berkeley DB (Ver 6.0.20, C#) under .NET Framework and Windows server 2008(x64). Any BDB application of win32 runs well but will encounter trouble under platform of x64 when compile BDB to x64, even though the DLL compiled and linked with win32. The bug is that Berkeley DB take amount of memory as the size of databases and regardless of cacheSize. My estimation is that all memory for BDB malloced and NOT freed.

  • WLS 8.1sp3 blocked threads halt MDB processing

    Under Weblogic Server 8.1 SP3 JMS consumers stop processing the JMS queues. This only started happening following our upgrade from SP2. We had numerous test baselines as well as months of production data to validate this.
              Specifically it appears that internal weblogic threads are blocked, preventing the MDBs (JMS consumers) from being dispatched. This doesn't immediately impact the user experience, but eventually unprocessed JMS messages accumulate and consume heap. You can figure out the rest.
              We had 5 MDBs working on this particular JMS queue, so under moderate traffic all 5 would need to be blocked before the problem was realized.
              As a work around, you'll need to request a patch from BEA. CR188391 appears to resolve this issue. You must request this however. BEA will not contact you - regardless of functional or economic impact.
              Thanks
              -Sean

    Sean,
              I've forwarded your comments internally. Usually, one can look in the release notes of future service-packs to see what's been fixed. I would think upgrading from SP3 to sp4, or even sp5 (the latest) would also have fixed the problem.
              Tom, BEA

  • Reuse a waiting/blocked thread

    Using the API in java.util.concurrency, there are operations such as BlockingQueue.take() or Condition.await() that makes the current thread to be blocked (waiting) on the operation to complete. I am working on a problem in which, in such situation, it is very costly to leave the thread as such holding all the resources already allocated for it. I would like to use the same thread to run/execute another task. And, when the condition or take can continue their work, then the resources are allocated back to the thread (including the local state) such that the thread can continue the execution from the blocked/waiting point. So, my question is that is there any way to release the thread when it is blocked on some concurrent operation such that it can be used for other executions?
    On a related ground, is there any way I can replace the Thread class implementation in Java/JVM?
    My current impression is that since some parts of the implementations are native so we need to tweak the C++ code, is that correct?
    Thanks in advance.

    i think you are missing the point of what i'm saying. i'm saying you don't need to change how threads work, you need to change your style of programming. having multiple tasks run on the same physical thread is as easy to do in java as it is in erlang (as Peter Lawrey described). what you need to do is use the erlang style of programming, where a thread does not block waiting for something to happen, but is instead giving a task when there is work today. i.e. you need to write your tasks so that they are essentially non-blocking. you can do this just fine in java without mucking w/ the jvm.
    for instance, say you have a standard model with 3 queues, 3 producers, and 3 consumers, where there is 1 producer and 1 consumer per queue, and the consumer is a dedicated thread. in this scenario, if a queue is empty, that thread is idle/useless. instead, you can change your model around so that instead of each producer putting "data" on the queue, they instantiate a "handler/consumer" (instance of Runnable) with a reference to the data to be handled. this handler is then tossed into a thread pool with 3 threads. in this scenario, your 3 threads are now "general purpose" and can work on whichever queue(s) happen to currently have data. (in essence, you are collapsing your 3 queues into 1 queue and making the queue data "smarter"). this is exactly what erlang is doing under the hood.
    Edited by: jtahlborn on Jul 28, 2011 11:34 AM

  • Is it possible to release a blocked thread in Java or by JNI through JVM?

    Expert please help me, or where can I refer to find the correct answer.
    Thank you.

    Try Thread.interrupt() and/or explicitly closing the resource that it is blocking on.

  • How to block thread until data is available?

    In my program i opened the socket and got inputStream. then i wanted to read the incoming data as it comes, so i wrote:
    byte[] data = new byte[2007];
    while (true) {
    int count = in.read(data);
    byte[] rcv = new byte[count];
    System.arraycopy(data,0,rcv,0,count);
    // ...process the data...
    }but it succesfully reads only first datablock, the next time method "read" returns -1 and the exception was raised (byte[-1] is a very weird thing). Which method should i use in order to block the thread until the data would came.

    Could you log if the Socket isConnected(), isClosed(), isBound(), and isInputShutdown() before a read?That won't help at all:
    - isConnected() returns 'true' forever after a socket has been connected.
    - isClosed() returns 'false' until this JVM closes this socket itself.
    - isBound() returns 'true' forever after a socket has been connected or bound.
    - isInputShutdown() returns 'false' until this JVM calls shutdownInput() on this socket itself.
    Contrary to common misconception, none of these methods tell you anything about the state of the connection or what the other end has done. They can't. There is nothing in TCP that can do that except (a) the -1 EOS indication and (b) IOException: connection reset.
    In this case it is clear that the peer has definitely closed the socket or shutdown its output.

  • Method to load images while blocking thread

    Hi!
    I'm new to Java and have been researching how to do the following for hours with no real success:
    I need a way to load an image (jpg/gif/what have you) while blocking the thread it is running in.
    I do not really want to use MediaTracker because it feels much too complicated for what I need. Here is a code snippet:
    private Thread getImages = new Thread(
              new Runnable(){
                   public void run() {
                        while(!imgNameList.isEmpty()){
    String imagePath = imgNameList.remove(0);
                             ImageIcon image = new ImageIcon(getDocumentBase(), imagePath);
                             if(image.getImageLoadStatus() == java.awt.MediaTracker.COMPLETE) {
                                  imgList.add(image);
                             This thread is initiated by my applet, and I just want it to do something very simple. I want it to load ALL the images from the URLs in imgNameList ArrayList<String> and if they are valid images, add them to the imgList ArrayList<ImageIcon>.
    I do not understand why my code does not work.
    I have read in multiple places that ImageIcon blocks the thread. But the condition mage.getImageLoadStatus() == java.awt.MediaTracker.COMPLETE is not ever true.
    I am totally exasperated by this inexplicable behaviour.
    Any advice will be greatly appreciated.
    Many thanks.

    Thanks for the replies.
    The URLs were correct.
    ImageIcon already implements MediaTracker by itself in the background, so I really don't want to have to do it myself.
    The status was always 4.. ERRORED.. even if the image loaded just fine anyway, as in I could display it after.
    I suspect it may be some strange thing due to Applet permissions or something of that sort. Eventually I gave up, so I just did as follows:
    ImageIcon to load the image.
    Get the height of the ImageIcon to do two things:
    A) Make sure that the image is actually loaded (not just returning right away like GetImage)
    B) Make sure it's a valid image (i.e. height greater than 0)
    When I have some time I'll look over this carefully but for now it works so it'll have to do.

  • Blocked thread with no events does not show up in Flight Recording

    When there is no event in a thread and the thread is blocked during the entire duration of a flight recording, the thread does not show up in the flight recording. How to check the state of such a thread from the flight recording?

    Hi,
    I am not sure I understand the question correctly, but events (like Java Blocked) are only saved to file when the thread is not longer blocked. So if you have an event that goes on during the whole recording it won't show up. It's a known limitation of flight recorder.
    If you are really desperate to find out the state of the thread you could look at the Method Profiling events that samples the thread.
    1) In the Event Types remove all the events that are checked.
    2) Check the Method Profiling Sample.
    3) Go to Events-> Threads. Select the thread you are interested in
    4) Right click and select Operative Set -> Set Selection
    5) Go to Events -> Log and check Show only Operative Set
    6) Click on an event in the log and look at Event Attributes below. There is a field called Thread State which will tell you the state thread.
    That's the best that you could do.
    Erik

  • (J2ME) Stopping a blocked thread

    In J2ME, receiving a datagram blocks the current thread until a datagram is received. I have created a thread that simply receives a datagram and then loops waiting for the next one. How do I stop the thread when it is blocking in datagram receive waiting for a datagram to come in? I am using the code on http://java.sun.com/j2se/1.3/docs/guide/misc/threadPrimitiveDeprecation.html for a reference, and it mentions calling Thread.interrupt() but this method does not exist in J2ME.

    I tried searching for any API on J2ME... it looked like there were a lot of different specifications, and in any case I never found any API docs.
    However, i did want to say this:
    In regular java, the way you would normally do this is (ThreadObj).interrupt(), but you said this didn't exist. Are you 100% sure?
    If that's not working, I have 2 other ideas:
    1. Thread.stop() - it has been deprecated and is not technically "safe" but it could probably work in a pinch.
    2. There is often a way to put a timeout on an inputstream. See if you can do that.

  • Blocking thread: i18n:localize tag

    Hi, my server was hung (wlp816). Took the thread dump. 15 threads are in blocking state. All of them got locked at the same line which is the <i18n:localize> tag line. Is there any special about this tag?
    Thanks
    "ExecuteThread: '69' for queue: 'weblogic.kernel.Default'" daemon prio=5 tid=0x043914e0 nid=0x67a3 runnable [0x2977d000..0x2977fc28]     
    at java.util.HashMap.get(HashMap.java:325)     
    at com.bea.p13n.taglib.i18n.JspMessageBundle.findInCache(JspMessageBundle.java:305)     
    at com.bea.p13n.taglib.i18n.JspMessageBundle.getBundle(JspMessageBundle.java:246)     
    at com.bea.p13n.taglib.i18n.LocalizeTag.determineLanguage(LocalizeTag.java:681)     
    at com.bea.p13n.taglib.i18n.LocalizeTag.doStartTag(LocalizeTag.java:585)
    at jsp_servlet._portlets._search._jspService(search.jsp:17)     
    at weblogic.servlet.jsp.JspBase.service(JspBase.java:33)     
    at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:1077)     
    at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:465)     
    at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:28)     
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:27)     
    at com.bea.p13n.servlets.PortalServletFilter.doFilter(PortalServletFilter.java:293)     
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:27)     
    at com.bea.wlw.netui.pageflow.PageFlowJspFilter.doFilter(PageFlowJspFilter.java:250)     
    - locked <0x91c6bca0>
    .......

    Can you be a bit more explicit about "doesnot seem
    to be working."
    Is the page throwing an error?
    Is it not finding your resource file?
    Is it not printing a string from it correctly?
    This JSTL code should iterate through all the keys in
    the loaded resource bundle and show you exactly what
    is in it.
    <c:forEach var="prop"
    items="${templatebundle.resourceBundle.keys}">
    <c:out value="${prop}"/>
    </c:forEach>Cheers,
    evnafetswell what is happening is that the the properties file has some properties defined aspage-message-logged-in=Logged in as
    page-label-login=Log In
    page-label-logout=Log Outand in the jsp I am accessing them as follows
    <fmt:message key="page-label-login" bundle="${templatesbundle}"/>but what I end up seeing in the jsp is the follows
    ???page-label-logout???

  • How can a thread kill another blocked thread?

    Hello All,
    As the title mentioned, lets say i have a main class in which i created a new thread but for some reason, this thread is blocked so i want to find a way to kill this thread from the main class (the interrupt function is not working), is there a workarround to do this?
    Thank you in advance

    Hello,
    First of all, thanks for you interventions, i think i should clearify my case:
    I have a website which has a "Search the site" feature where a user can search the site for specific keywords
    The search engine is an external jar which we use by calling its API, but for some reason this search engine becomes blocked sometimes so my jsp which is calling this API gets also blocked and any subsequent search gets also blocked in the end the number of java threads becomes very high in my system!
    i want to find a way so that if the piece of code calling the search API didnt get excuted during a specific period, i want to kill the thread calling the search API
    lets say the search form is submitted to the following jsp
    // Here we have a SearchThread extends Thread
    /* i launched a new Thread to do the search independatly from the main process*/
    SearchThread task = new SearchThread("search_input");
    task.start();
    /* In the searchThread class we have a run method containing the following line which calls the search API, for some reason , this line never returns and keeps the thread in Running state and so the run() method never returns and so the thread will neve be dead :( */
    SearchLoginSession = new SearchLoginSession (configFile, true);
    Notes:
    1) the stop is deprecated i cant use it
    2) the suspend method will suspend it and not kill it, the thread will remain in memory
    3) the interrupt method didnt work
    4)i am working in a multi-cpu environment
    Finally , what i want is to find a way to remove this thread from memory from the main jsp listed above like
    task.kill() or some workarround
    Thank you for reading and hope you can help me

  • Blocking Threads

    Hi All
    Can anyone explain, what exactly happens when a thread is blocked?
    I mean...when a thread is blocked, what happens to the object,class or the method ?
    Thanks,
    Akash

    a thread is blocked when its execution is suspended because another thread is executing.
    all the data, function stack frame is stored by java - its frozen. fyi a thread is not blocked when one line of code is executing.
    usually a whole function is allowed to run . we do not have to worry about it. except if we are trying to edit the same object from 2 concurrent threads - in which case we use synchronized

Maybe you are looking for