线程保持running状态的方式

   Java中常见的保持一个线程的生命的方式,前提是这个线程必须是non-daemon,只有非dead 的non-daemon的线程才能保证JVM进程不会退出。

   Java自身的线程池就是一个例子,ExecutorService,当调用submit(Runnable task)方法后,如果不显示的调用shutdown()方法,线程池线程是不会终止的。
  The ExecutorService does not kill the running threads, and since threads are created as non-daemon, the JVM doesn't exit.
   那么它是如何保持线程池线程的生命的呢?
   ExecutorService通过阻塞队列LinkedBlockingQueue来实现
  
import java.util.concurrent.LinkedBlockingQueue;

public class LinkedBlockingQueueDemo {

	public static void main(String[] args) throws InterruptedException {
		LinkedBlockingQueue queue = new LinkedBlockingQueue(10);
		System.out.println("debug1");
		System.out.println(queue.take());
		System.out.println("debug2");

	}

}

   


LinkedBlockingQueue的take()方法如下:
 public E take() throws InterruptedException {
        E x;
        int c = -1;
        final AtomicInteger count = this.count;
        final ReentrantLock takeLock = this.takeLock;
        takeLock.lockInterruptibly();
        try {
            while (count.get() == 0) {
                notEmpty.await();
            }
            x = dequeue();
            c = count.getAndDecrement();
            if (c > 1)
                notEmpty.signal();
        } finally {
            takeLock.unlock();
        }
        if (c == capacity)
            signalNotFull();
        return x;
    }


如果对类为空,它将被阻塞
while (count.get() == 0) {
                notEmpty.await();
  }

直到调用put(E e)方法被唤醒。


tomcat web服务器又是如何保持运行状态的呢,通过代码发现tomcat的启动类为
org.apache.catalina.startup.Bootstrap
Bootstrap依赖org.apache.catalina.startup.Catalina
Catalina依赖org.apache.catalina.core.StandardServer
通过StandardServer的public void await()实现,代码片段如下:
while(!stopAwait) {
                    try {
                        Thread.sleep( 10000 );
                    } catch( InterruptedException ex ) {
                        // continue and check the flag
                    }
}


还有一中方式保证一个线程的active,如下:
public class Main {

    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationProvider.xml" });
        
        context.start();
        System.out.println("按任意键退出");
        System.in.read();

    }

}

   因此可以总结出,保持一个线程的方式如下:
   1. 通过线程间通讯wait() 和notify()或者是Condition的awati()和signal()加上一个循环判断,例如ExecutorService
   2. 通过一个死循环。例如tomcat,为了减轻服务器负担,可以加上Thread.sleep( 10000 ); 让循环体执行得慢点。
   3.通过阻塞API的调用,例如IO流的read和write
  

猜你喜欢

转载自xls9577087.iteye.com/blog/2290687