多线程05.thread不知道的知识

       Thread thread = new Thread();
        //        init(null, null, "Thread-" + nextThreadNum(), 0);
      //init(线程组,策略名称,线程名称,栈大小);
        thread.start();
        System.out.println();
    }
1.创建线程对象Thread,默认有一个线程名,以Thread开头,从0开始计数。
Thread-0
Thread-1
Thread-2
Thread-3
2.如果在构造Thread的时候没有传递Runnable或者没有复写Thread的run方法
,该Thread将不会调用任何东西,如果传递了Runnable接口的实例,
或者复写了thread的run方法,则会执行该方法的逻辑代码。
3.如果构造对象时未传递ThreadGroup,
Thread会默认获取父线程的ThreadGroup作为该线程的ThreadGroup
此时子线程和父线程将会在同一个ThreadGroup中。
package chapter2;

import java.util.Date;

public class Demo01 {

    public static void main(String[] args) {
     /*   Thread thread = new Thread("myname"){
            @Override
            public void run() {
                try {
                    Thread.sleep(100l);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };*/
        //        init(null, null, "Thread-" + nextThreadNum(), 0);
//init(线程组,策略名称,线程名称,栈大小);
      //  thread.start();
        //thread.getThreadGroup().activeCount();
        ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
        System.out.println(threadGroup.activeCount());
        //获取线程数,如果不启动其他线程默认有两个线程,一个main线程,一个监听事件 Thread[Monitor Ctrl-Break,5,main]
        Thread[] threads = new Thread[threadGroup.activeCount()];
        threadGroup.enumerate(threads);
      for(Thread Th:threads){
          System.out.println(Th);
      }
    }
}

猜你喜欢

转载自www.cnblogs.com/q1359720840/p/10650137.html