Thread group: when a larger number of threads, and you can put together a similar thread when the function is relatively clear.
public class ThreadGroupName implements Runnable{ @Override public void run() { Thread thread = Thread.currentThread(); String name = thread.getThreadGroup().getName()+" : "+thread.getName(); while(true) { System.out.println("S9世界赛" + name); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { ThreadGroup group = new ThreadGroup("线程组"); Thread thread1 = new Thread(group, new ThreadGroupName(), "skt"); = New new Thread2 the Thread the Thread (Group, new new ThreadGroupName (), "FPX"); thread1.start (); thread2.start (); System.out.println ( "active threads:" + group.activeCount ()); // this value is an estimate, for reference group.list (); } }
Daemon thread is a special thread, it silently to complete some system services, java applications, if only daemon threads, JVM will automatically exit.
{class DaemonThread public public static myThread the extends the Thread class { @Override public void RUN () { the while (to true) { System.out.println ( "Daemon, Runing with the I AM!"); the try { the Thread.sleep (1000); } the catch (InterruptedException E) { // the TODO Auto-Generated Block the catch e.printStackTrace (); } } } } / ** * Y the following example, t1 is a daemon thread, the main thread is the main, when the main thread has finished running, the entire The program ends NATURAL * @param args * @throws InterruptedException * / public static void main (String [] args) {throws InterruptedException myThread new new myThread T1 = (); t1.setDaemon (true); // must () before setting the start, otherwise as a worker thread to perform t1.start (); Thread.sleep (2000); } }
Thread priority: in demanding environments, still needs its own application layer to solve the problem.
public class PrioRityThread { public static class LowThread extends Thread{ int count =0; @Override public void run() { while(true) { synchronized (PrioRityThread.class) { count++; if(count > 1000000) { System.out.println("LowThread count: " + count); break; } } } } } public static class HeiThread extends Thread{ int count =0; @Override public void run() { while(true) { synchronized (PrioRityThread.class) { count++; if(count > 1000000) { System.out.println ( "HeiThread COUNT:" + COUNT); BREAK; } } } } } public static void main (String [] args) { LowThread lowThread new new LowThread = (); HeiThread heiThread new new HeiThread = (); / / from one to ten priority, priority scheduling and the underlying operating system is closely related to a high priority does not necessarily perform! lowThread.setPriority (Thread.MIN_PRIORITY); heiThread.setPriority (Thread.MAX_PRIORITY); lowThread.start ( ); heiThread.start (); } }