Java 继承Thread类实现多线程:

package test;



import java.io.*;

import java.lang.*;

import java.util.*;



class tThread extends Thread {

	public tThread(String st) {

		super(st);

	}

	public void run(){

		for (int i = 0; i < 10; ++i){

			System.out.println(i + " " + this.getName());

			try{

				this.sleep((int)Math.random()*10);

			}

			catch(Exception e){

				System.out.println(e.toString());

			}

			

		}

	}

}



public class gbx12{

	public static void main(String args[]) {

		tThread r1 = new tThread("ME");

		tThread r2 = new tThread("You");

		r1.start();

		r2.start();

	}

}

  

你可能感兴趣的:(thread)