threaddemo3.java

来自「JAVA程序设计课程中各章节的程序实例。」· Java 代码 · 共 56 行

JAVA
56
字号
/**
*Threads implements Runnable interface
*2004.11.2. xhcprince
*/
class newThread implements Runnable
{
	Thread t;

	newThread()
	{
		t = new Thread(this,"Demo thread"); //如果不要this,则仅执行main thread!!!
		System.out.println("Child thread " + t); //this 代表当前进程,即子进程
		t.start();				//这里的Thread原形是这样的-public Thread(Runnable target, String name)
	}

	public void run()
	{
		try
		{
			for(int i=5; i>0; --i)
			{
				System.out.println("Child thread:" + i);
				Thread.sleep(1000);
			}
		}
		catch(InterruptedException e)
		{
			System.out.println("Child thread interrupted");
		}

		System.out.println("Child thread existing");
	}
}

class threadDemo3
{
	public static void main(String args[])
	{
		new newThread();

		try
		{
			for(int i=5; i>0; --i)
			{
				System.out.println("Main thread:" + i);
				Thread.sleep(1000);
			}
		}
		catch(InterruptedException e)
		{
			System.out.println("Main thread interrupted");
		}

		System.out.println("Main thread existing");
	}
}

⌨️ 快捷键说明

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