java:多线程(获取当前线程的对象)

版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/qq_24644517/article/details/84324949

 * Thread.currentThread(), 主线程也可以获取

public class Demo2_CurrentThread {

	public static void main(String[] args) {
		new Thread() {
			public void run() {
				System.out.println(getName()+"aaa");
			}
		}.start();
		
		
		new Thread(new Runnable() {			
			@Override
			public void run() {
				//currentThread获取当前正在执行的线程
				System.out.println(Thread.currentThread().getName()+"bbb");
			}
		}).start();
		Thread.currentThread().setName("我是主线程");					//获取主函数线程的引用,并改名字
		System.out.println(Thread.currentThread().getName());		//获取主函数线程的引用,并获取名字
	}

}

猜你喜欢

转载自blog.csdn.net/qq_24644517/article/details/84324949
今日推荐