标签:也会 守护线程 string reac 设定 err style 优先级 void
public class Demo { public static void main(String[] args) { Thread testThread = new Thread(() -> { System.out.println("testThread当前线程组名字:" + Thread.currentThread().getThreadGroup().getName()); System.out.println("testThread线程名字:" + Thread.currentThread().getName()); }); testThread.start(); System.out.println("执?main?法线程名字:" + Thread.currentThread().getNa } }
执?main?法线程名字:main
testThread当前线程组名字:main
testThread线程名字:Thread-0
线程优先级
public class Demo { public static void main(String[] args) { Thread a = new Thread(); System.out.println("我是默认线程优先级:"+a.getPriority()); Thread b = new Thread(); b.setPriority(10); System.out.println("我是设置过的线程优先级:"+b.getPriority()); } }
我是默认线程优先级:5
我是设置过的线程优先级:10
public class Demo { public static class T1 extends Thread { @Override public void run() { super.run(); System.out.println(String.format("当前执?的线程是:%s,优先级:%d", Thread.currentThread().getName(), Thread.currentThread().getPriority())); } } public static void main(String[] args) { IntStream.range(1, 10).forEach(i -> { Thread thread = new Thread(new T1()); thread.setPriority(i); thread.start(); }); } }
当前执?的线程是:Thread-17,优先级:9 当前执?的线程是:Thread-1,优先级:1 当前执?的线程是:Thread-13,优先级:7 当前执?的线程是:Thread-11,优先级:6 当前执?的线程是:Thread-15,优先级:8 当前执?的线程是:Thread-7,优先级:4 当前执?的线程是:Thread-9,优先级:5 当前执?的线程是:Thread-3,优先级:2 当前执?的线程是:Thread-5,优先级:3
如果某线程是守护线程,那如果所有的?守护线程结束,这个守护线程也会?动结束。
应?场景是:当所有?守护线程结束时,结束其余的?线程(守护线程)?动关闭,就免去了还要继续关闭?线程的麻烦。 ?个线程默认是?守护线程,可以通过Thread类的setDaemon(boolean on)来设置。
public static void main(String[] args) { ThreadGroup threadGroup = new ThreadGroup("t1"); threadGroup.setMaxPriority(6); Thread thread = new Thread(threadGroup,"thread"); thread.setPriority(9); System.out.println("我是线程组的优先级"+threadGroup.getMaxPriority()); System.out.println("我是线程的优先级"+thread.getPriority()); }
我是线程组的优先级6
我是线程的优先级6
标签:也会 守护线程 string reac 设定 err style 优先级 void
原文地址:https://www.cnblogs.com/mihich/p/13255009.html