extendsthread.java

来自「学了2个月java 练习用~ 可以看下 希望同意」· Java 代码 · 共 50 行

JAVA
50
字号
import java.io.*;

public class extendsThread
{
	public static void main(String[] args)
	{
		newThread one=new newThread("aaa");
		newThread two=new newThread("bb");
		one.setanother(two);
		two.setanother(one);

		one.start();
		two.start();
	}
}

class newThread extends Thread{
	newThread another;
	int i=1;

	public newThread(String name){
		super(name);
	}

	public void setanother(newThread now){
		another=now;
	}
	public void run (){
		for (i=1;i<=500 ;i++ ){

			System.out.println(getName()+" "+i+" "+another.isAlive());
			if(i==200&&another.isAlive()&&another.i!=200){
				try{
					another.join();
				}
				catch (Exception e){
				}
			}
		}
	}
	public void testjoin(){
		try{
			another.join();
		}
		catch (Exception e2){
			System.out.println(another.getName());
		}
	}
}

⌨️ 快捷键说明

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