我正在编写一个文件,可以在线拨打大量不同地点的电话(遗憾的是这是要求的一部分)
我的问题是这样的:
我们假设我有一个main方法,并想调用其他方法:
public static void main(String args[]){
//how do I break stuff into threads here
Thread nameThread1 = new(fileName.DoMethod1(x))
Thread nameThread2 = new(fileName.DoMethod2(x))
Thread nameThread3 = new(fileName.DoMethod3(x))
Thread nameThread4 = new(fileName.DoMethod4(x))
Thread nameThread5 = new(fileName.DoMethod5(x))
}
//fileName.java
public static void doMethod1(object){
//Do Method Stuff
}
public static void doMethod2(object){
//Do Method Stuff
}
我已经阅读了一些关于如何实现它的指南,但我仍然对这种方法的确切方法感到困惑。
有人可以告诉我一些例子吗?谢谢!
答案 0 :(得分:8)
你不能在帖子中调用不同的方法。你可以做的是打电话给不同的班级:
public static void main(String args[]){
Thread nameThread1 = new Thread(new Method1(x));
nameThread1.start();
Thread nameThread2 = new Thread(new Method2(x));
nameThread2.start();
Thread nameThread3 = new Thread(new Method3(x));
nameThread3.start();
Thread nameThread4 = new Thread(new Method4(x));
nameThread4.start();
Thread nameThread5 = new Thread(new Method5(x));
nameThread5.start();
}
public class Method1 implements Runnable {
private Object obj;
public Method1(Object obj) {
this.obj = obj;
}
//fileName.java
public void run(){
//Do Method Stuff
}
}
您应该始终考虑使用create ExecutorService
代码来管理此类作业。例如:
// create a thread pool with 10 workers
ExecutorService threadPool = Executors.newCachedThreadPool();
threadPool.submit(new Method1(x));
threadPool.submit(new Method2(x));
...
// once we have submitted all jobs to the thread pool, it should be shutdown
threadPool.shutdown();
如果您必须使用一个类,那么您可以启动一个使用开关或其他内容的Runnable
:
public static void main(String args[]){
Thread nameThread1 = new Thread(new Method(1, x));
nameThread1.start();
Thread nameThread2 = new Thread(new Method(2, x));
nameThread2.start();
...
}
public class Method1 implements Runnable {
private int which;
private Object obj;
public Method1(int which, Object obj) {
this.which = which;
this.obj = obj;
}
//fileName.java
public void run(){
switch(which) {
case 1:
doMethod1(obj);
break;
case 2:
doMethod2(obj);
break;
...
}
}
private void doMethod1(Object obj){
...
}
private void doMethod2(Object obj){
...
}
}
但执行者或单独的Runnable
类会更清晰。
答案 1 :(得分:1)
Thread
需要一个Runnable
的实例,所以你可以做的是创建一个实现Runnable的类,然后传递它必须响应的命令,然后根据你选择的命令打电话的方法。
public class MyRunnable implements Runnable {
public MyRunnable(String commandToExecute){
this.command = commandToExecute;
}
public void run() {
//depending on command call specific methods.
}
}
这种方法的一个缺点是你的run方法有太长的if else列表来检查要调用哪个方法。
好处是如果所有方法都与一个类相关并且将它们放在不同的类中没有意义,那么就不会创建不必要的类。
另一种方法是为Gray提供的每个方法都有单独的类,而不是直接创建特定类的实例,您可以使用Factory Pattern
为您创建和实例。
答案 2 :(得分:1)
如果你需要进行大量的调用,最好的解决方案是实例化一个线程池并将其用于所有任务。稍后,您可以根据计算机的处理器/核心数量进行调整。这是使用ThreadPoolExecutor
:
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class GenericThreadPool {
private static final int CORE_POOL_SIZE = 3;
private static final int MAXIMUM_POOL_SIZE = 10;
private static final long KEEP_ALIVE_TIME = 10;
private ThreadPoolExecutor threadPool = null;
private final LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
private GenericThreadPool() {
this.threadPool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS,
this.queue);
}
private static class LazyHolder {
public static final GenericThreadPool INSTANCE = new GenericThreadPool();
}
public static GenericThreadPool getInstance() {
return LazyHolder.INSTANCE;
}
public void execute(Runnable task) {
this.threadPool.execute(task);
}
public void shutdown() {
this.threadPool.shutdown();
}
}
要执行任务,您只需创建一个实现Runnable
接口的类,获取线程池实例并运行您的工作。
public class MyJob implements Runnable {
public void run(){
// Do your job here...
}
}
GenericThreadPool pool = GenericThreadPool.getInstance();
pool.execute(new MyJob());
答案 3 :(得分:0)
编写一个可调用的类,并根据请求params调用适当的方法,
私有类GetAndProcessResponse实现Callable { 私人最终请求请求; private final CountDownLatch countDownLatch;
GetAndProcessResponse(final Request request, final CountDownLatch countDownLatch) {
this.request = request;
this.countDownLatch = countDownLatch;
}
public ProcessedResponse call() {
try {
// do a switch case on request params
// and call appropriate methods.
} finally {
countDownLatch.countDown();
}
}
}
使用countDownLatch获取作业完成状态。
请勿调用join,因为您需要使用执行程序服务。
使用future.get(超时),因为它是一个网络作业,需要在一段时间后剪切。