/*
多线程示例。
创建多线程有两种方法。
一种继承Thread类,并且覆盖Thread中的run方法,并且调用其start方法。
startt方法的作用是开启线程,并且调用run方法。
复写run方法的目的是:将自定义的代码存储在run方法中,让线程运行。
*/
class Demo extends Thread{
public void run(){
for(int x =0; x <40;x++){
System.out.println("Demo Run...."+x);
}
}
}
class ThreadDemo{
public static void main(String[]args){
Demo d = new Demo();
d.start();//开启线程,并执行该线程的run方法。如果是d.run();则仅仅是允许run方法,并没有开启多线程。
for(int i = 0;i<40;i++){
System.out.println("helloWolrd----"+i);
} //主线程,与start开启的线程并行执行。
}
}本文出自 “司马囧” 博客,转载请与作者联系!
原文:http://9274590.blog.51cto.com/9264590/1739387