![](/img/trans.png)
[英]How can I make sure Provider<MyFacade> returns a new MyFacade in a new async thread?
[英]How can I make sure a thread is completed its task,then only new Thread can start in java?
我很惊讶如何知道某个线程成功完成了其任务,然后只允许另一个线程执行? 说明:假设我有10个模块。 但是线程应按顺序执行任务。 完全完成对一个模块的请求后,只有该请求才应跳至下一个模块。
下面是我的代码:
SiaThread.java
Collection <Modulo> modules = mppt.getModuloCollection();
for(Module module:modules){
UriThread threadUri = new UriThread(UriThread.URI_ENERGY_DAY,uriPath,"GET",semapore, module, gduSemapore,devType);
UriThread threadUriFault = new UriThread (UriThread.URI_FAILURE, uriPath, "GET", semapore, module, gduSemapore,devType);
}
UriThread.java
public UriThread (int iType, String strBaseUrl, String strmethod, Semaphore objsemaphore, Modulo module, GduSemaphore gduSemapore,String devType) {
this.iUriType = iType;
this.iRetry = 0;
this.strBaseUrl = strBaseUrl;
this.strMethod = strmethod;
this.semaphore = objsemaphore;
this.gduSemapore = gduSemapore;
this.modulo = module;
this.strResponse = "";
this.devType=devType;
setState (URI_FORMATION);
threadUrl = new Thread(this);
threadUrl.start();
}
该任务需要一个接一个地完成,如果线程尚未完成其任务,则应等待。
.Here semapore=new Semapore(10,true);
该问题的更多解释:一个SiaThread在其中运行,上面两个线程正在运行。 我希望SiaThread将等待在一个模块的循环中完成其任务,然后在循环中为另一个模块完成该任务。 请给我带代码的建议,以便我可以更好地理解。
您应该加入一个正在等待的线程。 您可以在Java官方文档中了解更多信息: http ://docs.oracle.com/javase/tutorial/essential/concurrency/join.html
从那里:“ join方法允许一个线程等待另一线程的完成。如果t是当前正在执行线程的Thread对象。”
如果要自己处理任务序列,请使用Thread.join()
。 或看看java.util.concurrent.Executors.newSingleThreadExecutor()
,它可以为您顺序处理任务。
对于给定的需求,请使用Thread.join()。 对于更高级的线程控制要求,请使用“同步,等待,通知”来实现监视器; 还应该评估更高级别的api(java并发包)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.