//继承实现多线程
package xian_cheng;
public class Example01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyThread myThread=new MyThread();//创建线程对象
myThread.start();//开启进程
while (true ) {
System.out.println("main()方法在运行");
}
}
}
class MyThread extends Thread{ //继承Thread类
public void run(){
while(true ){
System.out.println("MyThread类的run在运行");
}
}
}
//实现Runnable接口创建多进程
package xian_cheng;
public class Example03 {
public static void main(String[] args) {
// TODO Auto-generated method stub
/*start(),启动线程并执行相应的run方法
* run():子线程要执行的代码放入run()方法中
* currentThread(),静态的,获取当前的线程
* getName():获取此线程的名字
* setName():设置线程的名字
* join()线程插队
* isAlive()线程是否还存活
* sleep()线程睡眠
*
*/
MyThread2 myThread=new MyThread2();
Thread thread=new Thread(myThread);
thread.start();
while (true ) {
System.out.println("main()方法在运行");
}
}
}
class MyThread2 implements Runnable{ //实现runnable接口
//而且这个接口中只有一个run方法
public void run(){
while(true){
System.out.println("MyThread类的run在运行");
}
}
}