Java 如何从一个方法返回两种不同类型的值?
我需要从一个方法返回两种不同类型的变量(Future、ExecutorService)。我怎样才能前进??这是一段代码。。 我有一个类a,我试图调用“B”类中的run()方法startExecutorService'是启动threadExecutor和 “stopExecutorService”是停止threadExecutor的方法Java 如何从一个方法返回两种不同类型的值?,java,multithreading,methods,concurrency,executorservice,Java,Multithreading,Methods,Concurrency,Executorservice,我需要从一个方法返回两种不同类型的变量(Future、ExecutorService)。我怎样才能前进??这是一段代码。。 我有一个类a,我试图调用“B”类中的run()方法startExecutorService'是启动threadExecutor和 “stopExecutorService”是停止threadExecutor的方法 public class A{ public static void main(String[] args){ A AObj= new
public class A{
public static void main(String[] args){
A AObj= new A();
Future<?> task = null;
ExecutorService threadexec = null;
task = AObj.startExecutorService(task, threadexec);
AObj.stopExecutorService(task, threadexec);
}
public Future<?> startExecutorService(Future<?> control, ExecutorService threadExecutor){
//'noOfThreads' determines the total no of threads to be created in threadpool
int noOfThreads = 1;
// Creating an object for class 'B' which has the run() method
B ThreadTaskOne = new ExecutorServiceThreadClass();
// Calling the ExecutorService to create Threadpool with specified number of threads
threadExecutor = Executors.newFixedThreadPool(noOfThreads);
// Creating a Future object 'control' and thereby starting the thread 'ThreadTaskOne'
control = threadExecutor.submit(ThreadTaskOne);
return control;
}
public void stopExecutorService(Future<?> task, ExecutorService threadExecutor){
// Interrupting the thread created by threadExecutor
task.cancel(true);
if(task.isCancelled()){
System.out.println("Task has been cancelled !!");
}
// Closing the threadExecutor
threadExecutor.shutdownNow();
}
}
公共A类{
公共静态void main(字符串[]args){
A AObj=新的A();
未来任务=null;
ExecutorService threadexec=null;
task=AObj.startExecutorService(task,threadexec);
停止执行器服务(任务,线程执行器);
}
公共未来startExecutorService(未来控制,执行器服务线程执行器){
//“noOfThreads”确定要在线程池中创建的线程总数
int noOfThreads=1;
//为具有run()方法的类“B”创建对象
B ThreadTaskOne=新的ExecutorServiceThreadClass();
//调用ExecutorService以创建具有指定线程数的线程池
threadExecutor=Executors.newFixedThreadPool(noOfThreads);
//创建未来的对象“控件”,从而启动线程“ThreadTaskOne”
control=threadExecutor.submit(ThreadTaskOne);
返回控制;
}
public void stopExecutorService(未来任务,ExecutorService线程执行器){
//中断threadExecutor创建的线程
任务。取消(true);
if(task.isCancelled()){
System.out.println(“任务已取消!!”;
}
//关闭线程执行器
threadExecutor.shutdownNow();
}
}
我在“threadExecutor.shutdownNow();”行中的“stopExecutorService”方法处得到“NullPointerException”这个错误的原因是,
threadExecutor值在main方法中保持不变。。它仅在“startExecutorService”方法中更改。所以我想返回更改后的值
threadExecutor的返回主方法,以及未来
请帮助我创建一个复合类并返回该类的对象
class A{
}
class B{
}
class Composite{
A varA;
B varB;
public Composite(A ax, B by){ varA = ax; varB = by;}
public A returnA(){ return varA;}
public B returnB(){ return varB;}
}
class Executor{
A a;
B b;
////// code here
public Composite returnComposite{
Composite c = new Composite(a,b);
return c;
}
}
我认为您可能需要在标签中提供一些关于问题背景的提示。如果这与Java中的并发性有关,您可能应该添加标记“Java”、“并发性”或“多线程”。添加受欢迎的标签将吸引更多人关注您的问题。尽管如此,我不知道如何帮助你。谢谢你。。但是你能在a班给我一个建议,而不是去另一个班吗??因为这是我的要求,比如说。制作通用列表