如何使用 ExecutorService 运行 两个方法一个接一个地作为线程
How to run two methods as thread one after another using ExecuterService
我正在尝试执行两种方法,其中两种方法将在一段时间后一个接一个地执行,我正在使用 ExecuterService
。我已经实现了部分代码,但是直到现在我还没有实现全部功能,我在这里发布我的代码
public class ExampleExecuterService {
private static final int MYTHREADS = 3000;
public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(MYTHREADS);
Object methodList[]={
aMethod(),bMethod()
};
for(int i=0;i<methodList.length;i++){
Object myList = methodList[i];
Runnable worker = new MyRunnable(myList);
executor.execute(worker);
}
executor.shutdown();
// Wait until all threads are finish
while (!executor.isTerminated()) {
}
System.out.println("\nFinished all threads");
}
public static class MyRunnable implements Runnable {
private Object myList=null;
MyRunnable(Object myList) {
this.myList = myList;
}
@Override
public void run() {
try{
myList.wait(2000);
}catch(Exception e){
e.printStackTrace();
}
}
}
private static Object bMethod() {
System.out.println("This is inside method a ");
return null;
}
private static Object aMethod() {
System.out.println("This is inside method b ");
return null;
}
}
我想要 aMethod()
和 bMethod()
应该 运行 20 秒后 executer
将停止。如何用我的代码做到这一点。有人请帮助我。
我没有找到更好的解决方案来解决这中间等待的 20 秒,但是这样如何:
ExecutorService service = Executors.newSingleThreadExecutor();
service.submit(task1);
service.submit(() -> {
Thread.sleep(20000);
return null;
});
service.submit(task2);
Object methodList[]={
aMethod(),bMethod()
};
这不是您的方法列表。这是你的方法列表 return (=null).
在Java中,方法是不是对象。如果要将方法存储在列表或数组中,则必须将它们包装在对象中。通常的做法是使用 Runnable
界面或类似的东西。
在你的情况下,它可能看起来像这样:
Runnable[] methods = new Runnable[]{
new Runnable(){ // Object wrapper for method a
@Override
public void run(){ // method a
System.out.println("This is inside method a");
}
},
new Runnable(){ // Object wrapper for waiting
@Override
public void run(){ // method to wait for 20s
try{ Thread.sleep(20000); }catch(Exception e){}
}
},
new Runnable(){ // Object wrapper for method b
@Override
public void run(){ // method b
System.out.println("This is inside method b");
}
}
};
之后,您可以将这个 "methods" 数组提交给您的执行程序服务:
ExecutorService service = Executors.newSingleThreadExecutor();
for(Runnable r : methods)
service.submit(r);
service.shutdown();
但是,请记住 ExecutorService
主要是为了并发执行任务(=并行),而您希望按顺序执行它们。
你知道你总是需要一个顺序的、单线程的行为,你应该放弃 ExecutorService
并且简单地:
for(Runnable r : methods)
r.run();
编辑:完整的主要方法
public static void main(String[] args) throws Exception {
Runnable[] methods = new Runnable[]{
new Runnable(){ // Object wrapper for method a
@Override
public void run(){ // method a
System.out.println("This is inside method a");
}
},
new Runnable(){ // Object wrapper for waiting
@Override
public void run(){ // method to wait for 20s
try{ Thread.sleep(20000); }catch(Exception e){}
}
},
new Runnable(){ // Object wrapper for method b
@Override
public void run(){ // method b
System.out.println("This is inside method b");
}
}
};
ExecutorService service = Executors.newSingleThreadExecutor();
for(Runnable r : methods)
service.submit(r);
service.shutdown();
// Wait until all threads are finish
while (!service.isTerminated()) {}
System.out.println("\nFinished all threads");
}
我正在尝试执行两种方法,其中两种方法将在一段时间后一个接一个地执行,我正在使用 ExecuterService
。我已经实现了部分代码,但是直到现在我还没有实现全部功能,我在这里发布我的代码
public class ExampleExecuterService {
private static final int MYTHREADS = 3000;
public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(MYTHREADS);
Object methodList[]={
aMethod(),bMethod()
};
for(int i=0;i<methodList.length;i++){
Object myList = methodList[i];
Runnable worker = new MyRunnable(myList);
executor.execute(worker);
}
executor.shutdown();
// Wait until all threads are finish
while (!executor.isTerminated()) {
}
System.out.println("\nFinished all threads");
}
public static class MyRunnable implements Runnable {
private Object myList=null;
MyRunnable(Object myList) {
this.myList = myList;
}
@Override
public void run() {
try{
myList.wait(2000);
}catch(Exception e){
e.printStackTrace();
}
}
}
private static Object bMethod() {
System.out.println("This is inside method a ");
return null;
}
private static Object aMethod() {
System.out.println("This is inside method b ");
return null;
}
}
我想要 aMethod()
和 bMethod()
应该 运行 20 秒后 executer
将停止。如何用我的代码做到这一点。有人请帮助我。
我没有找到更好的解决方案来解决这中间等待的 20 秒,但是这样如何:
ExecutorService service = Executors.newSingleThreadExecutor();
service.submit(task1);
service.submit(() -> {
Thread.sleep(20000);
return null;
});
service.submit(task2);
Object methodList[]={ aMethod(),bMethod() };
这不是您的方法列表。这是你的方法列表 return (=null).
在Java中,方法是不是对象。如果要将方法存储在列表或数组中,则必须将它们包装在对象中。通常的做法是使用 Runnable
界面或类似的东西。
在你的情况下,它可能看起来像这样:
Runnable[] methods = new Runnable[]{
new Runnable(){ // Object wrapper for method a
@Override
public void run(){ // method a
System.out.println("This is inside method a");
}
},
new Runnable(){ // Object wrapper for waiting
@Override
public void run(){ // method to wait for 20s
try{ Thread.sleep(20000); }catch(Exception e){}
}
},
new Runnable(){ // Object wrapper for method b
@Override
public void run(){ // method b
System.out.println("This is inside method b");
}
}
};
之后,您可以将这个 "methods" 数组提交给您的执行程序服务:
ExecutorService service = Executors.newSingleThreadExecutor();
for(Runnable r : methods)
service.submit(r);
service.shutdown();
但是,请记住 ExecutorService
主要是为了并发执行任务(=并行),而您希望按顺序执行它们。
你知道你总是需要一个顺序的、单线程的行为,你应该放弃 ExecutorService
并且简单地:
for(Runnable r : methods)
r.run();
编辑:完整的主要方法
public static void main(String[] args) throws Exception {
Runnable[] methods = new Runnable[]{
new Runnable(){ // Object wrapper for method a
@Override
public void run(){ // method a
System.out.println("This is inside method a");
}
},
new Runnable(){ // Object wrapper for waiting
@Override
public void run(){ // method to wait for 20s
try{ Thread.sleep(20000); }catch(Exception e){}
}
},
new Runnable(){ // Object wrapper for method b
@Override
public void run(){ // method b
System.out.println("This is inside method b");
}
}
};
ExecutorService service = Executors.newSingleThreadExecutor();
for(Runnable r : methods)
service.submit(r);
service.shutdown();
// Wait until all threads are finish
while (!service.isTerminated()) {}
System.out.println("\nFinished all threads");
}