[英]How to check if a thread is still alive in tomcat if I know the thread name?
[英]Is posible to know if a certain Thread is alive?
為了我的html解析,我實現了一個生產者/消費者模式。 我的問題是我可以在生產者完成任務並且一個緩和的緩沖區之后停止消費者的方式。 例:
我有一個生產者和5個消費者。 生產者停止工作,因為已經完成了任務。 那么消費者怎么知道生產者已經完成了它的工作? 我嘗試實現:
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class MainTest {
public static void main(String[] args) {
BlockingQueue queue = new LinkedBlockingQueue<String>( );
Producer p = new Producer(queue,"0");
// FOR ALL CONSUMER I PASS THE REFERENCE TO THE UNIQUE PRODUCER !!!
Consumer c1=new Consumer(queue,"1",p);
Consumer c2=new Consumer(queue,"2",p);
Consumer c3=new Consumer(queue,"3",p);
Consumer c4=new Consumer(queue,"4",p);
Consumer c5=new Consumer(queue,"5",p);
p.start();
c1.start();c2.start();c3.start();c4.start();c5.start();
}
}
THE PRODUCER:
class Producer extends Thread{
BlockingQueue<String> queue;
public Producer(BlockingQueue<String> queue, String s) {
super ("THREAD"+s);
this.queue = queue;
}
@SuppressWarnings("deprecation")
public void run(){
int messagecode = 1;
boolean flagLavora= true;
//ciclo infinito
while(flagLavora ){
try {
System.out.println("-------------------------------------------------------------");
System.out.println(" : IS ALIVE "+Thread.currentThread().isAlive()+" ");
System.out.println(" IS INTERRUPTED "+Thread.currentThread().isInterrupted()+" ");
System.out.println( this.getName()+">>"+"PRODUTT Thread");
System.out.println("Producing "+(++messagecode));
queue.put("MESSAGE@"+messagecode);
System.out.println(messagecode+" in queue");
System.out.println("SIZE QUEUE:"+queue.size());
if (messagecode %9==0)
{
System.out.println( "STATE PRODUTT "+ Thread.currentThread().getState());
System.out.println( this.getName()+">>PRODUTT CLOSE");
System.out.println( "IS ALIVE:"+ this.currentThread().isAlive());
flagLavora= false;
}
System.out.println("-------------------------------------------------------------");
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println( "\n \n EXIT CICLE WHILE \n \n");
}
THE CONSUMER:
class Consumer extends Thread{
static int id;
static Producer produttore=null;
int code=0;
BlockingQueue<String> queue;
public Consumer(BlockingQueue<String> queue,String nameThread, Producer p) {
super ("THREADN_ "+nameThread);
this.produttore=p;
this.queue = queue;
code=++id;
}
public void run(){
while(true){
try {
System.out.println("\n -------------------------------------------------------------");
System.out.println("CONSUM "+this.getName()+":"+queue.size()+" SIZE QUEUE");
if (produttore==null){
stop();
}
else if ( produttore.getState().compareTo(State.RUNNABLE) ==0 || queue.size()!=0){
System.out.println( this.getName()+">> "+code+" waiting for the message...");
String message = queue.take();
System.out.println("Thread:"+code+" --> "+message+" taken!");
if ( produttore.getState().compareTo(State.RUNNABLE) ==0 || produttore!=null){
System.out.println("NULL PRODUTTORE + RUNNABLE");
System.out.println("CONSUMATORE:"+Thread.currentThread().getName() +" CHIUDE \n");
System.out.println("CONTENUTO PRODUTTORE: "+produttore==null);
stop();
}
System.out.println("------------------------------------------------------------- \n ");
//riposa 2 secondi
sleep(2000);
}
else {
sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
System.err.println("PROBLEMI CONSUMATORE:"+Thread.currentThread().getName());
}
}
}
所以我想知道如何在while中實現場景,生產者完成了添加隊列字符串的任務,並在完成后死亡,消費者工作直到隊列中的某些東西被分割並且消費者處於活着狀態。
誰能幫幫我? 謝謝
我不太明白。
您可以使用Thread.IsAlive()方法來了解您的生產者線程是否仍然存在。
你的while
會是這樣的:
while(produttore.isAlive() || !queue.isempty())
嘗試以其他方式解決問題。 取而代之的是阻塞隊列的,創建一個Executor
(使用輔助方法中Executors
)。
然后,對於每個任務,讓生產者將任務提交給執行者。 現在,您可以在生產者提交了所有任務之后簡單地等待執行程序終止( ExecutorService.awaitTermination()
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.