Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
When should I create a checked exception, and when should I make a runtime exception? For example, suppose I created the following class: public class Account { private float balance; /* ... constructor, getter, and other fields and methods */ public void transferTo(Account other, float amount) { if (amount > balance) throw new NotEnoughBalanceException();...
I am using the CountDownLatch to synchronize an initialization process between two threads and i was wondering about the proper handling of the InterruptedException that it might throw. the code i initially wrote was this: private CountDownLatch initWaitHandle = new CountDownLatch(1); /** * This method will block until the thread has fully initialized, this should only be called ...
Iam little bit confused about the use of yield() method in java in the following example .one of the concept with yiled() is 'used to prevent execution of thread'. My problems are 1.With and with out using yield method in the bellow code the same output is obtained.is it correct? 2.Actually what is the main use of yiled() 3.How it is different from join() and interrupt() methods publi...
Is there any possibility of exceptions to occur other than RuntimeException in Java? Thanks.
I have a thread: Thread t = new Thread(){ public void run(){ ServerSocketConnection scn = (ServerSocketConnection) Connector.open("socket://:1234"); // Wait for a connection. SocketConnection sc = (SocketConnection) scn.acceptAndOpen(); //do other operation } }; t.start(); Lets say no client is connecting to the Server, so this thread wi...
The Java documentation is not clear on this point. What happens if you call interrupt on a Thread before it calls Thread.sleep(): //interrupt reaches Thread here try { Thread.sleep(3000); } catch (InterruptedException e) { return; } Will the InterruptedException be thrown? Edit: If you know the answer can you please point me to relevan...
I'm developing simple survey service. I collect statistics in backend instance and dump them into datastore every hour. I registered shutdown hook for my backend instance so that data collected within last hour is not lost. I also use cache to store some of my data. Strangely call to MemcacheService.get() in LifecycleManager.ShutdownHook.shutdown() causes InterruptedException to be thrown. Her...
I want to implement something like this. 1.A background process which will be running forever 2.The background process will check the database for any requests in pending state. If any found,will assign a separate thread to process the request.So one thread per request.Max threads at any point of time should be 10. Once the thread has finished execution,the status of the request will be updat...
How can I make a thread run only if the other thread is running too, meaning, if I return from run in one thread, then I want the other to stop running too, my code looks something like this: ClientMessageHandler clientMessagehandler = new ClientMessageHandler(); ServerMessageHandler serverMessagehandler = new ServerMessageHandler(); Thread thread1 = new Thread(serverMessagehandler); ...
 /*
  * Copyright 1995-2005 Sun Microsystems, Inc.  All Rights Reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Sun designates this
  * particular file as subject to the "Classpath" exception as provided
  * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */
package java.lang;

Thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, either before or during the activity. Occasionally a method may wish to test whether the current thread has been interrupted, and if so, to immediately throw this exception. The following code can be used to achieve this effect:
  if (Thread.interrupted())  // Clears interrupted status!
      throw new InterruptedException();
 

public
class InterruptedException extends Exception {
    
Constructs an InterruptedException with no detail message.
    public InterruptedException() {
        super();
    }

    
Constructs an InterruptedException with the specified detail message.

Parameters:
s the detail message.
    public InterruptedException(String s) {
        super(s);
    }
New to GrepCode? Check out our FAQ X