Java用继承Thread类的方法,实现多线程

package per.thread;

public class MyThread extends Thread {

	private String name;
	
	public MyThread(String n){
		name = n;
	}
	
	public void run(){
		for (int i = 0; i < 5; i++) {
			System.out.println("线程开始:" + this.name + ",i=" + i);
		}
	}
	
	public static void main(String[] args) {
		
		MyThread mThread1 = new MyThread("猪");
		MyThread mThread2 = new MyThread("猪八");
		MyThread mThread3 = new MyThread("猪八戒");
		
		mThread1.start();
		mThread2.start();
		mThread3.start();
		
	}
}
线程开始:猪,i=0
线程开始:猪八戒,i=0
线程开始:猪八戒,i=1
线程开始:猪八戒,i=2
线程开始:猪八戒,i=3
线程开始:猪八戒,i=4
线程开始:猪八,i=0
线程开始:猪,i=1
线程开始:猪八,i=1
线程开始:猪,i=2
线程开始:猪八,i=2
线程开始:猪,i=3
线程开始:猪,i=4
线程开始:猪八,i=3
线程开始:猪八,i=4

猜你喜欢

转载自blog.csdn.net/qq_38006520/article/details/82934926