欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用Runnable实现多线程

程序员文章站 2022-03-02 19:32:01
...
import java.util.Date;


//Runnable 接口实现多线程
public class  Thread_RunnableTest implements Runnable {

	private int pauseTime;
	private String Threadname;
	public Thread_RunnableTest(int x,String y){
		this.pauseTime = x;
		this.Threadname = y;
	}

	
	public void run() {
		// TODO Auto-generated method stub
		for(int i=0;i<10;i++){
			try {
				System.out.println(Threadname+":"+new Date(System.currentTimeMillis()));
				Thread.sleep(pauseTime);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		


	}

	//测试线程
	public static void main(String[] args){
		//创建线程 1  
		Thread t1 = new Thread(new Thread_RunnableTest(1000,"我是1线程"));
		t1.start();

		//创建线程 2 
		Thread t2 = new Thread(new Thread_RunnableTest(5000,"我是2线程"));
		t2.start();
	}

}