thread state and java.lang.IllegalThreadStateException exceptions for Java
article structure:
1 clarifying the six states of the Java Declaration cycle
2 because of its own operation error, the same thread instance is called at the same time two times the start method to throw java.lang.IllegalThreadStateException this exception
3 Super Brother take you to analyze why throw this anomaly
4 Super brother want to see praise haha
1 clarifying the six states of the Java Declaration cycleThere are six thread life cycles, represented by the Thread.state enumeration class, the state of the thread can be queried using the GetState method, and the new thread is created as follows, but the Start method is not called runnable The thread is running or can run when the operating system dispatches it blocked thread is blocked, waiting for the lock waiting thread to invoke the wait method of object or thread's join method without running timed_waiting Thread calls the sleep method of the thread class or adds a delay call to object's Wait method or thread's join method terminated thread Run method normal or throw exception end here the Model class is the thread class that inherits thread
2 The sample code for the exception that was written by the Super brother
Public Static voidMain (string[] args) {model model=NewModel (); Model.start (); Model.start (); }classModelextendsthread{intnum = 20; @Override Public voidrun () {//TODO auto-generated Method Stub while(num>0) {System.out.println (Thread.CurrentThread (). GetName ()+" "+num); Num--; } }}
Run results
Exception in thread "main" java.lang.IllegalThreadStateException
At Java.lang.Thread.start (Unknown Source)
At TeachMultiThread.teach1.main (teach1.java:7)
Thread-0 20
Thread-0 19
Thread-0 18
Thread-0 17
Thread-0 16
Thread-0 15
Thread-0 14
Thread-0 13
Thread-0 12
Thread-0 11
Thread-0 10
Thread-0 9
Thread-0 8
Thread-0 7
Thread-0 6
Thread-0 5
Thread-0 4
Thread-0 3
Thread-0 2
Thread-0 1
For the same thread class, you cannot call the Start method at the same time two times, and the state of the model class changes after the Start method is called.
3 Super Brother take you to analyze why throw this anomalyWhen we call the Start method on the model this instance, the state of the model thread instance has changed to the state of runnable, and when we call the Start method again, the state of course throws
illegalthreadstateexceptionThis is abnormal we can test the public class teach1 { public static void. main (String[] args) { model model = new model (); model.start (); system.out.println (Model.getstate ()); }}class model extends thread{ int num = 20; @ Override public void run () { // TODO Auto-generated method stub while (num>0) { system.out.println ( Thread.CurrentThread (). GetName () + " " +num); num--; } }}thread-0 20runnablethread-0 19thread-0 18Thread-0 17Thread-0 16Thread-0 15Thread-0 14Thread-0 13Thread-0 12thread-0 11thread-0 10thread-0 9thread-0 8thread-0 7thread-0 6thread-0 5thread-0 4thread-0 3thread-0 2thread-0 1 can see that the above output is printed runnable
4 Super brother want to see praiseGuys
, give your brother a good-for-all .
Take you to play the Java Multithreading series one java.lang.IllegalThreadStateException exception