Java uses the java. Lang. Thread class to implement multithreading. Let's take a look at the two methods of thread creation and running through the same example:
(1) One is to declare the sub-class of thread, and to overload the Thread class method run (extend java. Lang. Thread and override the run method)
class MyThread extends Thread{public void run(){for(int row = 1;row <5;row ++){for(int i = 0;i < 5;i ++)System.out.print('*');System.out.println();}}}public class TestNew {public static void main(String[] args) {// TODO Auto-generated method stubMyThread mt = new MyThread();mt.start();}}
Execution result:
*****
*****
*****
*****
(2) Another way is to declare a class that implements the runnable interface. Then implement the run (implement java. Lang. runnable and implement the run method) method)
class MyThread implements Runnable{public void run(){for(int row = 1;row <5;row ++){for(int i = 0;i < 5;i ++)System.out.print('*');System.out.println();}}}public class TestNew {public static void main(String[] args) {// TODO Auto-generated method stubThread myThread = new Thread(new MyThread());myThread.start();}}
The running result is the same as above. It should be noted that the thread itself only executes once, unless there is a circular Processing Mechanism in it.
Http://blog.csdn.net/duoshanx/archive/2004/08/28/87279.aspx ()