threadtester.java

来自「Java语言是目前最流行」· Java 代码 · 共 36 行

JAVA
36
字号
//继承Thread类
class CountThread extends Thread
{
	private	int start;//发起
	private int finish;//完成
	
	public CountThread(int from,int to)
	{//构造函数
		this.start = from;
		this.finish = to;
	}
	public void run()
	{
		System.out.println(this.getName()+" static executing...");
		for(int i=start;i<=finish;i++)
		{
			System.out.print(i + "  ");	
		}	
		System.out.print(this.getName()+" finished executing.");
	}
}
public class ThreadTester
{
	static public void main(String[] args)
	{
		CountThread thread1 = new CountThread(1,10);
		CountThread thread2 = new CountThread(20,30);
		thread1.start();
		thread2.start();
	}	
}
//运行时两次结果不一样
/*
	1次、1和2线程一起运行
	2次、1线程结束后2线程才运行。
*/

⌨️ 快捷键说明

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