Friday, March 13, 2015

IllegalThreadStateException

java.lang.IllegalThreadStateException

IllegalThreadStateException is described in the javadoc comments as:

Thrown to indicate that a thread is not in an appropriate state for the requested operation. See, for example, the suspend and resume methods in class Thread.
author: unascribed version: 1.21, 12/19/03 see: java.lang.Thread#resume() see: java.lang.Thread#suspend() since: JDK1.0

Where is this exception thrown?

Following, is a list of exception messages cross-referenced to the source code responsible for throwing them. Click on the method link to view the code and see how the exception is thrown.

How is this exception thrown?

The following sub-sections identify where this exception is thrown, and how (or why) the code is throwing the exception.

Any source code quoted in this section is subject to the Java Research License unless stated otherwise.

java.lang.Thread.setDaemon(boolean)

/**
     * Marks this thread as either a daemon thread or a user thread. The 
     * Java Virtual Machine exits when the only threads running are all 
     * daemon threads. 
     * This method must be called before the thread is started. 
     * This method first calls the <code>checkAccess</code> method 
     * of this thread 
     * with no arguments. This may result in throwing a 
     * <code>SecurityException </code>(in the current thread). 
     * @param      on   if <code>true</code>, marks this thread as a
     *                  daemon thread.
     * @exception  IllegalThreadStateException  if this thread is active.
     * @exception  SecurityException  if the current thread cannot modify
     *               this thread.
     * @see        java.lang.Thread#isDaemon()
     * @see          #checkAccess
     */
public final void setDaemon(boolean on) {
    checkAccess();
    if (isAlive()) {
        throw new IllegalThreadStateException();
    }
    daemon = on;
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.lang.Thread.start()

/**
     * Causes this thread to begin execution; the Java Virtual Machine 
     * calls the <code>run</code> method of this thread. 
     * The result is that two threads are running concurrently: the 
     * current thread (which returns from the call to the 
     * <code>start</code> method) and the other thread (which executes its 
     * <code>run</code> method). 
     * It is never legal to start a thread more than once.
     * In particular, a thread may not be restarted once it has completed
     * execution.
     * @exception  IllegalThreadStateException  if the thread was already
     *               started.
     * @see        java.lang.Thread#run()
     * @see        java.lang.Thread#stop()
     */
public synchronized void start() {
    if (started) throw new IllegalThreadStateException();
    started = true;
    group.add(this);
    start0();
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.lang.ThreadGroup.add(Thread)

/**
     * Adds the specified Thread to this group.
     * @param t the Thread to be added
     * @exception IllegalThreadStateException If the Thread group has been destroyed.
     */
void add(Thread t) {
    synchronized (this) {
        if (destroyed) {
            throw new IllegalThreadStateException();
        }
        if (threads == null) {
            threads = new Thread[4];
        } else if (nthreads == threads.length) {
            Thread newthreads[] = new Thread[nthreads * 2];
            System.arraycopy(threads, 0, newthreads, 0, nthreads);
            threads = newthreads;
        }
        threads[nthreads] = t;
        nthreads++;
        nUnstartedThreads--;
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.lang.ThreadGroup.add(ThreadGroup)

/**
     * Adds the specified Thread group to this group.
     * @param g the specified Thread group to be added
     * @exception IllegalThreadStateException If the Thread group has been destroyed.
     */
private final void add(ThreadGroup g) {
    synchronized (this) {
        if (destroyed) {
            throw new IllegalThreadStateException();
        }
        if (groups == null) {
            groups = new ThreadGroup[4];
        } else if (ngroups == groups.length) {
            ThreadGroup newgroups[] = new ThreadGroup[ngroups * 2];
            System.arraycopy(groups, 0, newgroups, 0, ngroups);
            groups = newgroups;
        }
        groups[ngroups] = g;
        ngroups++;
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.lang.ThreadGroup.addUnstarted()

/**
     * Increments the count of unstarted threads in the thread group.
     * Unstarted threads are not added to the thread group so that they
     * can be collected if they are never started, but they must be
     * counted so that daemon thread groups with unstarted threads in
     * them are not destroyed.
     */
void addUnstarted() {
    synchronized (this) {
        if (destroyed) {
            throw new IllegalThreadStateException();
        }
        nUnstartedThreads++;
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.lang.ThreadGroup.destroy()

/**
     * Destroys this thread group and all of its subgroups. This thread 
     * group must be empty, indicating that all threads that had been in 
     * this thread group have since stopped. 
     * First, the <code>checkAccess</code> method of this thread group is 
     * called with no arguments; this may result in a security exception. 
     * @exception  IllegalThreadStateException  if the thread group is not
     *               empty or if the thread group has already been destroyed.
     * @exception  SecurityException  if the current thread cannot modify this
     *               thread group.
     * @see        java.lang.ThreadGroup#checkAccess()
     * @since      JDK1.0
     */
public final void destroy() {
    int ngroupsSnapshot;
    ThreadGroup[] groupsSnapshot;
    synchronized (this) {
        checkAccess();
        if (destroyed || (nthreads > 0)) {
            throw new IllegalThreadStateException();
        }
        ngroupsSnapshot = ngroups;
        if (groups != null) {
            groupsSnapshot = new ThreadGroup[ngroupsSnapshot];
            System.arraycopy(groups, 0, groupsSnapshot, 0, ngroupsSnapshot);
        } else {
            groupsSnapshot = null;
        }
        if (parent != null) {
            destroyed = true;
            ngroups = 0;
            groups = null;
            nthreads = 0;
            threads = null;
        }
    }
    for (int i = 0; i < ngroupsSnapshot; i += 1) {
        groupsSnapshot[i].destroy();
    }
    if (parent != null) {
        parent.remove(this);
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.awt.Robot.checkNotDispatchThread()

private void checkNotDispatchThread() {
    if (EventQueue.isDispatchThread()) {
        throw new IllegalThreadStateException('Cannot call method from the event dispatcher thread');
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

No comments:

Post a Comment