线程保持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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值