java多线程是否处于活动状态
通过这种方式,我生成线程,但在for循环之后,如何检查所有线程是否处于活动状态java多线程是否处于活动状态,java,multithreading,Java,Multithreading,通过这种方式,我生成线程,但在for循环之后,如何检查所有线程是否处于活动状态 进一步的处理取决于所有线程 如果线程是死线程,则进行进一步的处理 for (int i = 0;i<List.size();i++) { if(List.get(i).trim().length() >0 ){ Thread thread = new Thread("Thread"+i){
进一步的处理取决于所有线程
如果线程是死线程,则进行进一步的处理
for (int i = 0;i<List.size();i++) {
if(List.get(i).trim().length() >0 ){
Thread thread = new Thread("Thread"+i){
public void run(){
threading(List.get(i),ctx,userSesionObj);
}
};
thread.start();
}
}
for(int i=0;i0){
螺纹=新螺纹(“螺纹”+i){
public void run(){
线程(List.get(i)、ctx、usersesionbj);
}
};
thread.start();
}
}
这里线程化
是我的函数,它对我传递的不同数据执行使用类中的isAlive方法
从文件中:
测试此线程是否处于活动状态。如果线程已启动但尚未死亡,则该线程是活动的
从类中使用isAlive方法
从文件中:
测试此线程是否处于活动状态。如果线程已启动但尚未死亡,则该线程是活动的
您可能想要加入线程。查阅假设您可以挂起主线程,也就是说。您可能想要加入线程。查阅假设您可以挂起主线程,也就是说。您可以使用。事实上,我强烈推荐这个。您可以指定不同的线程行为等。执行器将为每个提交的线程返回一个对象,然后您可以遍历每个
Future
以收集结果。因此,以下是:
for (Future f : futures) {
f.get(); // get a result here...
}
当所有的期货
返回数据时(即线程完成时),将完成。Executor API的工作级别高于join()
/notify()
等,使用起来更安全
这里有很多功能可供使用,我推荐。您可以使用。事实上,我强烈推荐这个。您可以指定不同的线程行为等。执行器将为每个提交的线程返回一个对象,然后您可以遍历每个Future
以收集结果。因此,以下是:
for (Future f : futures) {
f.get(); // get a result here...
}
当所有的期货
返回数据时(即线程完成时),将完成。Executor API的工作级别高于join()
/notify()
等,使用起来更安全
这里有很多功能可供使用,我推荐。正如Peter所建议的,这应该可以
int count = selectedCategoryList.size();
List<Thread> threads = new ArrayList<Thread>();
for (int i = 0; i < count; i++) {
if(selectedCategoryList.get(i).trim().length() >0 ){
final Long CategoryID = Long.parseLong(selectedCategoryList.get(i));
Thread thread = new Thread("ReplaceMedicationThread"+i){
public void run(){
threadingForReplacingMedication(CategoryID,ctx,userSesionObj);
}
};
threads.add(thread):
}
}
//start them in a loop
//join them in a loop
//move on....
int count=selectedCategoryList.size();
List threads=new ArrayList();
for(int i=0;i0){
final Long CategoryID=Long.parseLong(selectedCategoryList.get(i));
线程线程=新线程(“替换药物线程”+i){
public void run(){
用于更换药物的螺纹(类别ID、ctx、userSesionObj);
}
};
线程。添加(线程):
}
}
//以循环方式启动它们
//把他们围成一个圈
//继续。。。。
正如彼得所建议的,这应该可以
int count = selectedCategoryList.size();
List<Thread> threads = new ArrayList<Thread>();
for (int i = 0; i < count; i++) {
if(selectedCategoryList.get(i).trim().length() >0 ){
final Long CategoryID = Long.parseLong(selectedCategoryList.get(i));
Thread thread = new Thread("ReplaceMedicationThread"+i){
public void run(){
threadingForReplacingMedication(CategoryID,ctx,userSesionObj);
}
};
threads.add(thread):
}
}
//start them in a loop
//join them in a loop
//move on....
int count=selectedCategoryList.size();
List threads=new ArrayList();
for(int i=0;i0){
final Long CategoryID=Long.parseLong(selectedCategoryList.get(i));
线程线程=新线程(“替换药物线程”+i){
public void run(){
用于更换药物的螺纹(类别ID、ctx、userSesionObj);
}
};
线程。添加(线程):
}
}
//以循环方式启动它们
//把他们围成一个圈
//继续。。。。
使用执行器服务
-执行器
很好地管理线程
对象和异步
任务
-这里不是直接运行线程的客户端,而是中间对象
-将此方法与submit()
方法相结合,该方法返回一个Future
对象
-未来
是异步任务的结果
-您可以使用未来的
get()
方法,该方法具有阻塞性质,或者get()
方法与isDone()
方法一起使用。使用执行器服务
-执行器
很好地管理线程
对象和异步
任务
-这里不是直接运行线程的客户端,而是中间对象
-将此方法与submit()
方法相结合,该方法返回一个Future
对象
-未来
是异步任务的结果
-您可以使用未来的
get()
方法,该方法具有阻塞性,或者get()
方法以及isDone()
方法。在继续处理之前等待某些线程完成的一种简单方法是通过CompletionService。以下是您将如何使用此选项:
// list of data that need to be processed before continuing execution
List<DataForProcessing> workList = // ...
// An instance of an Executor http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Executors.html
Executor exec = // ...
// asume that ResultData is a data structure returned from the processing each
// DataForProcessing instance. If processing does not return a value you could
// parameterise the completion service as CompletionService<Void>.
CompletionService<ResultData> service = new ExecutorCompletionService<ResultData>(executor);
// Submit the tasks for processing. Each task will be processed by the executor and
// its result will be wrapped in a Future and place in a BlockingQueue maintained by
// the CompletionService instance. Again, if your submitted task does not return
// a value you should parameterise your task as Callable<Void>.
for (DataForProcessing data: workList) {
service.submit(new Callable<ResultData>() {
public ResultData call() {
// process the data here and return some result (optionally).
}
});
}
// retrieve the results as the become available.
for (int i = 0; i < workLoad.size(); i++) {
// Retrieve the next processed result. CompletionService returns each task in a
// Future which provides you with the means to control the lifecycle of the task.
// When a submitted task is processed by the executor, its Future is placed on a
// BlockingQueue maintained by the CompletionService. Invoking take() will return
// the next available result. If your tasks do not return a result you would still
// need to call Future<Void> f = service.take() in order to wait for every task
// to finish before proceeding.
Future<ResultData> f = service.take();
// If your task returns some result you may access it via the get() method on the
// task's Future.
ResultData result = f.get();
// do some processing if needed
process(result);
}
//继续执行前需要处理的数据列表
列表工作列表=/。。。
//遗嘱执行人的实例http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Executors.html
执行者执行=/。。。
//asume表明ResultData是从每次处理返回的数据结构
//DataForProcessing实例。如果处理未返回值,则可以
//将完成服务参数化为CompletionService。
CompletionService=新的执行者CompletionService(执行者);
//提交要处理的任务。每个任务都将由执行者和
//它的结果将在将来被包装并放入由
//完成服务