📌  相关文章
📜  如何解决Java主线程中的Java .lang.IllegalStateException?

📅  最后修改于: 2022-05-13 01:54:25.758000             🧑  作者: Mango

如何解决Java主线程中的Java .lang.IllegalStateException?

扰乱程序正常流程的无例外、不需要的事件称为Exception

大多数时候异常是由我们的程序引起的,这些都是可以恢复的。示例:如果我们的程序要求是从位于美国的远程文件中读取数据 在运行时,如果远程文件不可用,那么我们将得到 RuntimeException 说 fileNotFoundException。如果发生fileNotFoundException,我们可以将本地文件提供给程序以正常读取并继续程序的其余部分。

Java的异常主要有以下两种:

1.检查异常:

为使程序在运行时顺利执行而由编译器检查的异常称为检查异常。在我们的程序中,如果有可能出现检查异常,那么我们应该处理该检查异常(通过 try-catch 或 throws 关键字),否则我们将得到编译时错误。



已检查异常的示例有 ClassNotFoundException、IOException、SQLException 等。

2. 未经检查的异常:

未经编译器检查的异常,无论程序员是否处理此类异常,都称为未检查异常。

未检查异常的示例是 ArithmeticException、ArrayStoreException 等。

IllegalStateException是 RuntimeException 的子类,因此它是一个未经检查的异常。此异常由程序员或 API 开发人员显式引发,以指示在错误的时间调用了方法。通常,此方法用于指示在非法或不适当的时间调用方法。

示例:启动一个线程后,我们不允许再次重新启动同一个线程,否则我们会得到运行时异常,说 IllegalStateException。

示例 1:当它已经在执行 run() 方法时,我们调用 start() 方法。



Java
// Java program to show the occurrence of
// IllegalStateException.
 
// Import required packages
 
import java.io.*;
import java.util.*;
 
// Creating a thread in our myThread class by extending the
// Thread class
// class 1
// Helper class
 
class myThread extends Thread {
   
    // Method in helper class
    // declaring run method
    public void run()
    {
 
        for (int i = 0; i < 5; i++) {
           
            // Display message
            System.out.println("GeeksForGeeks");
        }
    }
}
 
// class 2
// Main class
class Thread1 {
   
    // Main driver method
    public static void main(String[] args)
    {
        // creating a thread object  in the main() method
        // of our helper class above
        myThread t = new myThread();
       
        // Starting the above created thread
        // using the start() method
        t.start();
       
        // Display Message
        System.out.println("Main Thread");
       
        // starting the thread again when it is already
        // running and hence it cause an exception
        t.start();
    }
}


Java
// Java program to show the occurrence of
// IllegalStateException.
 
// Import required packages
import java.io.*;
import java.util.*;
 
// Creating a thread in our myThread class by extending the
// Thread class
// class 1
// Helper class
class myThread extends Thread {
   
    // Method in helper class
    // declaring run method
    public void run()
    {
 
        for (int i = 0; i < 5; i++) {
           
            // Display message
            System.out.println("GeeksForGeeks");
        }
    }
}
 
// class 2
// Main class
class Thread1 {
   
    // Main driver method
    public static void main(String[] args)
    {
        // creating a thread object  in the main() method
        // of our helper class above
        myThread t = new myThread();
       
        // Starting the above created thread
        // using the start() method
        t.start();
       
        try {
            System.out.println("Main Thread is going to sleep");
           
            // making main thread sleep for 2000ms
            t.sleep(2000);
            System.out.println("Main Thread awaken");
        }
        catch (Exception e) {
            System.out.println(e);
        }
 
        // Display Message
        System.out.println("Main Thread");
       
        // calling start( ) method on a dead thread
        // which causes exception
        t.start();
    }
}


Java
// Java program to demonstrate that the error
// does not occure in this program
 
// Import required packages
import java.io.*;
import java.util.*;
 
// Creating a thread in our myThread class by extending the
// Thread class
// class 1
// Helper class
class myThread extends Thread {
   
    // Method in helper class
    // declaring run method
    public void run()
    {
 
        for (int i = 0; i < 5; i++) {
           
            // Display message
            System.out.println("GeeksForGeeks");
        }
    }
}
 
// class 2
// Main class
class Thread1 {
   
    // Main driver method
    public static void main(String[] args)
    {
        // creating a thread object  in the main() method
        // of our helper class above
        myThread t = new myThread();
       
        // Starting the above created thread
        // using the start() method
        t.start();
       
        try {
            System.out.println("Main Thread is going to sleep");
           
            // making main thread sleep for 2000ms
            t.sleep(2000);
           
            System.out.println("Main Thread awaken");
        }
        catch (Exception e) {
            System.out.println(e);
        }
 
        // Display Message
        System.out.println("Main Thread");
    }
}



示例 2:当线程执行完 run() 方法后,我们在线程上调用 start() 方法。

Java

// Java program to show the occurrence of
// IllegalStateException.
 
// Import required packages
import java.io.*;
import java.util.*;
 
// Creating a thread in our myThread class by extending the
// Thread class
// class 1
// Helper class
class myThread extends Thread {
   
    // Method in helper class
    // declaring run method
    public void run()
    {
 
        for (int i = 0; i < 5; i++) {
           
            // Display message
            System.out.println("GeeksForGeeks");
        }
    }
}
 
// class 2
// Main class
class Thread1 {
   
    // Main driver method
    public static void main(String[] args)
    {
        // creating a thread object  in the main() method
        // of our helper class above
        myThread t = new myThread();
       
        // Starting the above created thread
        // using the start() method
        t.start();
       
        try {
            System.out.println("Main Thread is going to sleep");
           
            // making main thread sleep for 2000ms
            t.sleep(2000);
            System.out.println("Main Thread awaken");
        }
        catch (Exception e) {
            System.out.println(e);
        }
 
        // Display Message
        System.out.println("Main Thread");
       
        // calling start( ) method on a dead thread
        // which causes exception
        t.start();
    }
}


如何解决这个错误?

为了避免Java主线程中的Java .lang.IllegalStateException,我们必须确保我们代码中的任何方法都不会在非法或不适当的时间被调用。

在上面的例子中,如果我们只在线程 t 上调用 start() 方法一次,那么我们不会得到任何Java.lang.IllegalStateException 因为我们没有在线程启动后调用 start() 方法(即我们没有调用 start () 方法在非法或不适当的时间。)

Java

// Java program to demonstrate that the error
// does not occure in this program
 
// Import required packages
import java.io.*;
import java.util.*;
 
// Creating a thread in our myThread class by extending the
// Thread class
// class 1
// Helper class
class myThread extends Thread {
   
    // Method in helper class
    // declaring run method
    public void run()
    {
 
        for (int i = 0; i < 5; i++) {
           
            // Display message
            System.out.println("GeeksForGeeks");
        }
    }
}
 
// class 2
// Main class
class Thread1 {
   
    // Main driver method
    public static void main(String[] args)
    {
        // creating a thread object  in the main() method
        // of our helper class above
        myThread t = new myThread();
       
        // Starting the above created thread
        // using the start() method
        t.start();
       
        try {
            System.out.println("Main Thread is going to sleep");
           
            // making main thread sleep for 2000ms
            t.sleep(2000);
           
            System.out.println("Main Thread awaken");
        }
        catch (Exception e) {
            System.out.println(e);
        }
 
        // Display Message
        System.out.println("Main Thread");
    }
}