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

📄 threaddemo.java

📁 JAVA程序设计课程中各章节的程序实例。
💻 JAVA
字号:
//Using isAlive and Join
class newThread implements Runnable
{
	Thread t;
	String name;

	newThread(String threadname)
	{
		name = threadname;
		t = new Thread(this,name);
		System.out.println("New Thread " + this.name);
		t.start();
	}

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

		System.out.println(name + "Existing");
	}
}

class ThreadDemo
{
	public static void main(String args[])
	{
		newThread ob1 = new newThread("one");
		newThread ob2 = new newThread("two");
		newThread ob3 = new newThread("three");

		System.out.println(ob1.t.isAlive());
		System.out.println(ob2.t.isAlive());
		System.out.println(ob3.t.isAlive());

		try
		{
			ob1.t.join();
			ob2.t.join();
			ob3.t.join();
		}
		catch(InterruptedException e)
		{
			System.out.println("Main thread");
		}

		System.out.println(ob1.t.isAlive());
		System.out.println(ob2.t.isAlive());
		System.out.println(ob2.t.isAlive());
	}
}

⌨️ 快捷键说明

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