Dowemo
0 0 0 0

1, let's look at the running results of the following program

public class Thread2 {
public static void main(String[] args) {
 myThread m=new myThread();
 m.start();
 System.out.println("运行结束");
}
}
class myThread extends Thread {
 @Override
 public void run() {
//重新Thread的run方法
 super.run();
 System.out.println("MyThread");
 }
}

Depending on the number of times you run, the results are different:
MyThread
运行结束

运行结束
MyThread

From the running results, we can get it. When using multithreading, the execution of code is independent of the execution order of code. A thread is a child task that invokes a thread 's run method in an indeterminate manner. If we do more than one of the above programs, we'll find something. We change the code to the following:
public class Thread2 {
public static void main(String[] args) {
 myThread m=new myThread();
 while(true){
 m.start();
 System.out.println("运行结束");
 }
}
}
class myThread extends Thread {
 @Override
 public void run() {
//重新Thread的run方法
 super.run();
 System.out.println("MyThread");
 }
}
Run results:
运行结束
MyThread
Exception in thread"main" java.lang.IllegalThreadStateException
 at java.lang.Thread.start(Thread.java:705)
 at com.demo1.vector.Thread2.main(Thread2.java:7)

And why do you see this problem.
Because our custom thread inherits the thread class, the same thread class cannot call the start ( ) method because the thread 's lifecycle is a process.




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs