How-to interrupt a hanging methodcall

I wonder if someone from you now, how
one can interrupt a method while it is hanging for some
reason.
This is the problemdescription is as following:
If the method has longer then 10 seconds, it needs to be interrupted.
Any ideas?
Thanx, nils

Thanx for your code. But the problem is extended:
I should get a value from the "sort-of" server. So I did the following:
public class Main
     public static void main(String[] args)
          Client c = new Client();
          Server s = new Server();
          ControlThread ct = new ControlThread(s,c, 100);
          ct.run();
* This class gets a server and a client. The
* client needs a value that is returned by the server.
* But the server can hang while evaluating that value, so
* the ControlThread has a timeout thread that bypasses that
* call if the timeout occures.
* @author n_birkeland
class ControlThread{
      * @param Server this is the server that has a method that can hang
      * @param Client this client needs a value from the server
      * @param timeout this is the time the control waits until it
      * bypasses the call to the client
     ControlThread(Server s, Client c, int timeout){
          this.s = s;
          this.c = c;
          this.timeout = timeout;
     public void run(){
          try{
               Thread invoker = new Thread(){
                    public void run(){
                         value = s.get();
                         doneWork();
               Thread timer = new Thread(){
                    public void run(){
                         try{
                              sleep(timeout);
                         }catch(Exception e){;}
                         finally{
                            reachedTimeout();                         
               invoker.start();
               timer.start();
               // wait, until one flag is set to true:
               while( isTimeoutReached!=true && isWorkDone!=true ){
                    // busy waiting...
               System.err.println("exited busy waiting: isTimeoutReached="+isTimeoutReached+" isWorkDone="+isWorkDone);
               if(!isWorkDone){
                    value = -1;
               c.set(value);
               invoker.interrupt();
               timer.interrupt();
          }catch(Exception e){
               System.err.println(""+e);                    
               c.set(-1);
     synchronized void doneWork(){
          isWorkDone = true;
          System.out.println("done work: isTimeoutReached="+isTimeoutReached+" isWorkDone="+isWorkDone);          
     synchronized void reachedTimeout(){
          isTimeoutReached = true;
          System.out.println("reached timeout: isTimeoutReached="+isTimeoutReached+" isWorkDone="+isWorkDone);          
     int value = 0;
     boolean isWorkDone = false;
     boolean isTimeoutReached = false;
     final int timeout;
     final Server s;
     final Client c;
class Server{
     final int HANG_TIME = 300;
     int get(){
          try{
               System.out.println("server is going to sleep...");
               Thread.sleep(HANG_TIME);
          }catch(Exception e){;}     
          return 1987234; // some fancy value
class Client{
     void set(int value){
          System.out.println("Got value from server: "+value);
}

Similar Messages

  • How do you create hanging indents in Pages?

    How do you create hanging indents in Pages?

    Look at the left hand margin indicator -
    Compare it to the right-hand one. You'll note that it is actually two stops, instead of just one - a small bar on top of a triangle.
    Split them by careful dragging, and you can get what you want -
    You may need to do a bit of fiddling until you get it just the way you want it.

  • How to kill the hanged JFrame in swing?

    How to kill the hanged frame in swing?
    I am opening multiple JFrame and working on them.These frames are plcaed in JDesktopPane.
    If one frame is hanged up then i could not work on others .
    I need to kill the hanged frame.
    Assist me.

    am opening multiple JFrame and working on them.These frames are plcaed in JDesktopPaneWell, a JFrame, can't be added to a JDesktopPane, so your question doesn't even make sense.
    As was suggested earlier, if your GUI is unresponsive, then that means you are blocking the GUI EDT and the GUI can't respond to events. Don't do this.
    Read the Swing tutorial on Concurrency to understand why this happens and to learn the proper way to write GUI code so it won't happen.

  • How do you make hanging indents on the iPad version of pages?

    I'm trying to write a paper that needs to be in APA format which includes hanging indents. How do you make hanging indents on the iPad version of pages? I don't want to have to convert it into word every time I need to write a paper or fix the formatting

    Create a paragraph style which has the margins and paragraph indent where you want them.
    http://help.apple.com/pages/ipad/2.0/#/tan72487ce9
    Once you've done it for one paragraph and got that looking right you can copy and paste that style onto other paragraphs:
    http://help.apple.com/pages/ipad/2.0/#/tana9e5281c1
    Sooner or later you should probably learn to use the help function on your expensive device and software.

  • How to resolve the hang-up of TNT4882 after write-transfer

    Hi,
        My GPIB device based on TNT4882  can correctly receive the data sent from my VISA app using viPrintf, and my GPIB device can also write data to my VISA app using viRead through GPIB, like "Acery,XXXXX,xxxxxxxxx,2.5-5.20-23". But Everytime my VISA app always poped up an TIMEOUT error on the function of viRead and I showed the maybe receiving data, my VISA app can show the correct data "Acery,XXXXX,xxxxxxxxx,2.5-5.20-23". Not only the VISA app pops up the TIMEOUT error, but also that my gpib program running on my device could not detect the termination condition like END,EOS,ERR,B_done,B_tlcint after it sent out the data .  I have no idea on how to solve this problem so far.
        The GPIB program for sending out data  in Programed I/O mode is coded following the example of ESP-488 (http://joule.ni.com/nidu/cds/view/p/id/223/lang/en) in the dir of ESP-488\NOTNT\NGPIB_IO.c and EX5.c)  as an example. I cannot find any defect about the register programming.
    Now I would show the write-data code sequence:
             When I detect the TNT4882 is in Talker mode from ADSR, I will call the initializing writing code for writing-data-transfer:
    TNT_Out(R_cmdr,F_resetfifo);
      TNT_Out(R_cnt0, (char)(twos_cnt));      /* Load twos compliment count     */
      TNT_Out(R_cnt1, (char)(twos_cnt>>8));   /*  into TNT count registers      */
      TNT_Out(R_cnt2, (char)(twos_cnt>>16));
      TNT_Out(R_cnt3, (char)(twos_cnt>>24));
      TNT_Out(R_imr0,B_glint);              /* Set write to imr0 to be sure   */
      TNT_Out(R_auxmr,HR_auxrj|0);          /*   B_to is cleared              */
      TNT_Out(R_imr1, B_err);         /* End transfer on err            */
      TNT_Out(R_eosr, 0);        /* Set EOS byte                   */
      TNT_Out(R_auxmr,HR_auxra|F_hlda)
      TNT_Out(R_cfg , (B_tlchlte | CCEN ));  // I don`t set time-out attribute.
      TNT_Out(R_auxmr,F_hldi);               /* Hold off immediately           */
      TNT_Out(R_cmdr, F_go);
    During the writing-data-transfer, I use the status of END|EOS|ERR|B_done|B_tlcint to
    control the data transfer. All the data is written to FIFOB for sending out.
           After aborting transfer of writing:
     TNT_Out(R_cmdr,F_stop);
     TNT_Out(R_cmdr,F_resetfifo);
     if(TNT_In(R_isr1)&B_end)                /* If we received an END          */
        TNT_Out(R_auxmr,F_clrEND);            /*   Clear status bit             */
     if(TNT_In(R_isr1)&B_err) {
        TNT_Out(R_auxmr,F_clrERR);            /* Clear error bit                */
     if(io_type==OUTPUT) {
          TNT_Out(R_hssel,F_onechip|B_go2sids); /* if error set to idle state.    */
    Now I showed up some registers of TNT4882 during different phase.
    registers after initialization of write-transfer and before write-transfer:
    isr0 0x21
    isr1 0x0
    isr3 0x48
    adsr ox4a
    sasr 0x70
    sts1 0x10
    sts2 0x9a
    bsr  0x21
    registers after write-transfer:
    isr0 0x21
    isr1 0x2
    isr3 0x48
    adsr ox4a
    sasr 0x70
    sts1 0x10
    sts2 0x9a
    bsr  0x21
    registers after a few seconds(TNT was still Talker):
    isr0 0x21
    isr1 0x0
    isr3 0x48
    adsr ox4a
    sasr 0x70
    sts1 0x10
    sts2 0x9a
    bsr  0x31
           After write-transfer, my device program cannot detect any condition of ERR,END,EOS,DONE,TLCINT in
    isr0,isr1,isr2, isr3 to do the post-termination action.
           As you can see, some registers above almost donn`t change. I really donn`t know how to solve the
    TIMEOUT problem and how to notify my VISA app that the transfer is completed.
           At last could you give me an example of how to initialize the write-transfer?

    Can you try below, maybe it is helpful for you:
    This error may occurs when the interrupt line (IRQ) of the AT-GPIB/TNT interface is not configured properly. This incorrect configuration makes it so that the interrupts generated by the GPIB hardware are not detected by the GPIB driver. The GPIB hardware cannot notify the software that any operation is complete. Make sure the appropriate IRQ value was used when installing the module:
    If you are using a non plug and play AT-GPIB/TNT, make sure that the jumper settings match the value used when configuring the driver module.
    If you are using a plug and play AT-GPIB/TNT, make sure the value in the file generated by the isadump utility matches the one used when configuring the driver module.
    In both cases, if the appropriate IRQ value was used when configuring the driver module, try changing the IRQ value used. Remove the module and re-install with the new resource settings. For more information on configuring the resources for plug and play AT-GPIB/TNT interfaces, refer to the README and README.PNP text files included with the nigpib package.
    Li Yi

  • How to interrupt an funtion call and continue the followed code?

    for example, the code:
    public class A{
    public static void main(String[] args){
    int i = 0;
    method1();
    system.out.println("exit");
    public void method1(){
    int j = 0;
    while(true){
    j +=1;
    there is a error in method1(), so the call the method1( ) in main() will make an error.
    how can i interrupt this funtion call and continue the followed code?

    in actually, I can not control the method1(), that's
    an remote object function.
    So I can not add exception in methoes().Then in your main method catch whatever "Error" your method1 is throwing.

  • How to Interrupt an Unblocked Thread

    Consider the following code:
    public class MyThread extends Thread {
      public void run() {
        //long operation, might NOT be a blocking one, say
       long lineno = 1;
       System.out.println(" Line Number: " + lineno++);
       System.out.println(" Line Number: " + lineno++);
       System.out.println(" Line Number: " + lineno++);
        :   //say some 1000+ such LOC is there. i.e there's no loop/blocking etc
       System.out.println(" Line Number: " + lineno++);
    in some other class
    MyThread obj = new MyThread();
    obj.start();..at some later point, I want to terminate the (long time consuming) operation which is being perfomred by thread (obj) ...
    will the following code work?
    obj = null;What exactly the above code will do to the thread (obj)?
    I know there's one method
    obj.interrupt();but AFA my understanding with this method is that it works best if we have some kind of loops in our run method,
    so that we can periodically check the status whether thread is being interrupted or not, or something similer.
    So how can we interrupt/terminate such thread??

    You cannot safely, forcibly stop a running thread.For code in original post (by vijaywadnere) .stop() should work
    yes it's deprecated and bad idea in general, but it will do
    consider example
    public static void main(String[] args) {
              final Thread obj = new Thread() {
                     public void run() {
                       //long operation, might NOT be a blocking one, say
                      long lineno = 1;
                      while(true){                                   
                      System.out.print(" . " + lineno++);                 
              Thread stopper = new Thread(obj) {
                     public void run() {
                          try {
                             sleep(100);
                             obj.interrupt();
                             System.out.println("\ninterrupt");
                             sleep(100);
                             obj.stop();
                             System.out.println("\nstop");
                        } catch (InterruptedException e) {
                             // TODO Auto-generated catch block
                             e.printStackTrace();
              System.out.println(" obj");
              obj.start();
              System.out.println(" stopper");
              stopper.start();
    }

  • How to interrupt SSLServerSocket.accept()???

    Hi all,
    I have created a simple SSL Server socket, and I called 'accept()' in a separate thread, however, this method cannot be interrupted with Thread.interrupt(). It just blocks and stays that way. For non-SSL sockets, I know I can use channels, but I could not find the SSL-equivalent implementation.
    Any ideas how to imlement an interruptible SSL server socket?

    There is no SSLServerSocketChannel apart from my product at http://www.telekinesis.com.au, but you only need to set a timeout on the SSLServerSocketChannel. This will cause accept() to throw a SocketTimeoutException, and every time it does it can inspect the Thread.isInterrupted() flag. How long the timeout should be is up to you, i.e. how quickly you need the interrupt to take effect.

  • How to interrupt running subvi with global variation

    Hi,all
       I want to use global variation STOP to interrupt the running VI at anytime, but the subVI must be running until executing the WHILE. I knew when one action is  operated in the EVENT case, the other action will not be iperated. My question is how to solve the problem without  POP subVI and the DATA transmitted to MAIN panel. Give me a suggestion, THKS!
      Lioncsu  
    Attachments:
    Main VI.jpg ‏43 KB
    Sub VI.jpg ‏41 KB

    Hi Lioncsu,
          In Main, when a Start event occurs, the Value of Stop (False?) is immediately written to the Global (once), and SubVI starts running.  The Start event case won't complete - or write another value to the Global - until the SubVI is done.  I haven't run this, but I think pressing Stop may cause the SubVI to run (again) - for one iteration! 
    Attached is a simple VI which uses two loops - one to handle GUI events (the buttons) and one to start a while loop.  Maybe you can replace the while-loop with your SubVI?
    Hope it helps!
    "Inside every large program is a small program struggling to get out." (attributed to Tony Hoare)
    Attachments:
    Notifiers.vi ‏51 KB

  • HT1414 how to recover my hanged iphone

    my iphone was hanging frequently. when i take to mobile store they recovering my iphone through system. can i do it through my laptop. and how?

    Was the Support Article you linked from not Helpful..?
    Use iTunes to restore your iOS device to factory settings

  • How to stop Mail hanging on open message?

    I am running Mac OS X 10.8.2 on a new (mid 2012) MacBook Air 11 inch.
    I am having a strange problem: I cannot seem to close an open window inside
    Mail, which displays an email.  I've tried Force Quit several times, but no luck.
    The event which triggered this was Latexian (a LaTeX editor), which crashed.
    It generated a crash report, and opened a window in mail to which the report
    was attached and addressed to [email protected]  However, I cannot
    send the mail, or close the window.
    In Activity Monitor it is possible to send a signal to Mail process: Hangup,
    Interrupt, Quit, Abort etc.  Will one of these do the trick?
    If this doesn't work, I'm going to have to take this in to the Genius Bar.
    Thanks for any help.
    Sandeep.

    Thanks John,
    The "When all else fails - pull the plug" method does work here. And I can see the logic behind your answer.
    As this is not a frequent occurrence (thank heavens! - probably a bad hair day), I'll not take you up on the suggestion to remove the send button from view - but I appreciate your thoroughness.
    Is there no software-driven way to remove mail from the sending mode?

  • How to interrupt a process ?

    I have started a process to transfer files from my pc to my imac and it seems to be blocked. Howcan I interrupt it ?

    It's best to let it run, however you can always Quit Migration Assistant or if you have to Force Quit it.

  • How to interrupt sound output?

    Hello,
    I need help with one problem. I have VI that plays waveform. And I need to interrupt it in any moment during playing, and to perform new playing with new waveform. When I use Sound Output  Stop and Sound Output Clear in different combinations, Sound Output Write is stopped and program drops down.
    Can somebody help? 

    Hi,
    I mean that program paused due to Sound Write pause. And the only way to continue dataflow is Play Sound function. But I need to interrupt playing and perform new one with new settings. I've attached two screenshots to clear situation.
    Attachments:
    Sound11.jpg ‏26 KB
    Sound21.jpg ‏28 KB

  • How to interrupt MDB processing

    Hi
    We have a problem with web service call from MDB. Due to some network problems the call can hang for 30 minutes and any no way of setting timeout on JAX-WS port nor sun.net works. Whats more there is a silent transaction timeout on JTA (with no exception) and when web service call returnes after such a long time with success, the message is rolled back to JMS queue.
    I wonder if there is any wat to set timeout on MDB processing that would casue exception and let us control this behaviour. When there is more messages like described above it causes our queue processing to stall and gives us real headache !
    please help

    Thank you a lot for replies
    You both are right.
    As we went down to implementation details it appeared that there is no way (at lest not brutal way) to stop MDB processing. I believe that fiddling with threads in appserver environment is asking for troubles, so I decided to find the source of first problem: no timeout to WS processing.
    To our suprise it was caused by... migration from NetBeans 6.5 to 6.7. Somehow build scripts were affected and some jars were added to ejb jar, which were not there while using NB 6.5. Some of these jars were JAX-WS jars.
    It seams that timout does not work when using JAX-WS shipped with NB 6.7 but there is no problem using libraries shipped with Glassfish 2.1.
    When we removed JAX-WS from our deployment we got controls of timeouts back in our hands.
    thank you again

  • How to turn off hanging numerals in Photoshop?

    Is here any way to manually change numerals to non-hanging and vice versa? I noticed Ps automatically changes numerals to hanging when in a givven string of characters there's a mathematical sign, i.e. +  / -
    Is there any way to override that?

    This is just a guess but since you are editing a YouTube video frame, the pixels are not square and PS has automatically set a non-square viewing mode.  This is useful if the frame you are editing will be going back into the video, but not useful if you want to save it as a still image.
    There are several Pixel Aspect Ratio options on the View menu.
    Here is someone with the same question in 2005:
    http://forums.adobe.com/message/6257869#6257869

Maybe you are looking for