Java 如何使用ExecuterService将两个方法作为线程依次运行
我正在尝试执行两个方法,其中两个方法将在一段时间后逐个执行,我正在使用Java 如何使用ExecuterService将两个方法作为线程依次运行,java,multithreading,executorservice,Java,Multithreading,Executorservice,我正在尝试执行两个方法,其中两个方法将在一段时间后逐个执行,我正在使用ExecuterService。我已经实现了代码的一部分,但是到目前为止我还不能实现全部功能,我在这里发布我的代码 public class ExampleExecuterService { private static final int MYTHREADS = 3000; public static void main(String[] args) throws Exception { ExecutorServ
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;
}
}
公共类ExampleExecuterService{
私有静态最终整数=3000;
公共静态void main(字符串[]args)引发异常{
ExecutorService executor=Executors.newFixedThreadPool(MYTHREADS);
对象方法列表[]={
aMethod(),b方法()
};
对于(int i=0;i我没有找到一个更好的解决方案来等待中间的20秒,但是这个怎么样:
ExecutorService service = Executors.newSingleThreadExecutor();
service.submit(task1);
service.submit(() -> {
Thread.sleep(20000);
return null;
});
service.submit(task2);
我没有找到一个更好的解决方案来等待20秒之间的时间,但是这个怎么样:
ExecutorService service = Executors.newSingleThreadExecutor();
service.submit(task1);
service.submit(() -> {
Thread.sleep(20000);
return null;
});
service.submit(task2);
这不是您的方法列表。这是您的方法返回(=null)的列表
在Java中,方法是而不是对象。如果要将方法存储在列表或数组中,则必须将它们包装在对象中。通常的方法是使用可运行的接口或类似的方法
在您的情况下,它可能如下所示:
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");
}
}
};
之后,您可以将此“方法”数组提交给executor服务:
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");
}
这不是您的方法列表。这是您的方法返回(=null)的列表
在Java中,方法是而不是对象。如果要将方法存储在列表或数组中,则必须将它们包装在对象中。通常的方法是使用可运行的接口或类似的方法
在您的情况下,它可能如下所示:
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");
}
}
};
之后,您可以将此“方法”数组提交给executor服务:
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");
}
在cMethod()中将aMethod()和bMethod()组合在一起有问题吗?在cMethod()中将aMethod()和bMethod()组合在一起有问题吗这中间有一个20秒的睡眠时间?但这里我将如何运行方法a和BB,但我应该在哪里编写代码?你能编辑我的问题吗?然后这将非常有用OK我在我的帖子中添加了完整的main方法,但这里我将如何运行方法a和BB,但我应该在哪里编写代码?你能编辑我的问题吗?然后这将非常有用OK我添加了在我的帖子里有完整的主要方法