⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testthreadone.java

📁 thread进程调用
💻 JAVA
字号:
class OneThread implements Runnable{
	
	Thread t;
	
	OneThread(){
		t=new Thread(this,"New Thread");
		t.start();
	}
	
	public void run(){
		try{
			for(int i=0;i<7;i++){
				System.out.println("1 Thread: "+i+"   "+System.nanoTime());//当前系统时间,时间单位为毫秒
				t.sleep(3000); //线程休眠时间
			}
		}catch(InterruptedException e){
			e.printStackTrace();
		}
	}
}

class TwoThread extends Thread{
	
	TwoThread(){
		super("Two Thread");
		//设置线程优先级
		this.setPriority(5);
		start();
	}
	
	public void run(){
		try{
			for(int i=0;i<7;i++){
				System.out.println("2 Thread: "+i+"   "+System.nanoTime());
				Thread.sleep(3000);
			}
		}catch(InterruptedException e){
			e.printStackTrace();
		}
	}

}

public class TestThreadOne {
	public static void main(String[] args) {
		OneThread sec=new OneThread();
		
		TwoThread trd=new TwoThread();
		
		Thread t=Thread.currentThread();
		long startTime = System.nanoTime();
		long runTime = System.nanoTime() - startTime;//runTime运行时间

		try{
			for(int i=0;i<7;i++){
				System.out.println("0 Thread: "+i+"    "+runTime );
				Thread.sleep(3000);
			}
			sec.t.join();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -