java多线程------实现多线程两种方式

//继承实现多线程
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在运行");
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值