黑马程序员技术交流社区

标题: 多线程的启动以及获取线程对象名字 [打印本页]

作者: 木子小四    时间: 2016-4-14 13:07
标题: 多线程的启动以及获取线程对象名字
三种形式:
=====================
  1. package com.heima.test;

  2. public class Test3 {

  3.         /**
  4.          * @param args
  5.          */
  6.         public static void main(String[] args) {
  7.                 Thread.currentThread().setName("我是主线程");                                        //主线程
  8.                 System.out.println(Thread.currentThread().getName());
  9.                
  10.                 new Thread(){                                                                                                //线程1,通过Thread()
  11.                         public void run() {
  12.                                 this.setName("haha");
  13.                                 Thread.currentThread().setName("ze..ze");
  14.                                 System.out.println(Thread.currentThread().getName());
  15.                         }
  16.                 }.start();
  17.                
  18.                
  19.                 new Thread(new Runnable() {                                                                        //线程2,通过Ruannable()
  20.                         public void run() {
  21.                                 Thread.currentThread().setName("heihei");
  22.                                 System.out.println(Thread.currentThread().getName());
  23.                         }
  24.                 }).start();
  25.         }
  26.        
  27. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2