小编N10*_*N10的帖子

如何正确关闭java ExecutorService

我有一个简单的java ExecutorService运行一些任务对象(实现Callable).

ExecutorService exec = Executors.newSingleThreadExecutor();
List<CallableTask> tasks = new ArrayList<>();
// ... create some tasks
for (CallableTask task : tasks) {
 Future future = exec.submit(task);
 result = (String) future.get(timeout, TimeUnit.SECONDS);
 // TASKS load some classes and invoke their methods (they may create additional threads)
 // ... catch interruptions and timeouts
}
exec.shutdownNow();
Run Code Online (Sandbox Code Playgroud)

在完成所有任务(DONE或TIMEOUT-ed)之后,我尝试关闭执行程序,但它不会停止:exec.isTerminated() = FALSE. 我怀疑某些被执行的任务未正确终止.

是的,我知道执行者的关闭不能保证任何事情:

除尽力尝试停止处理主动执行任务之外,没有任何保证.例如,典型的实现将通过{@link Thread#interrupt}取消,因此任何未能响应中断的任务都可能永远不会终止.

我的问题是,有没有办法确保这些(任务)线程终止?我提出的最佳解决方案是System.exit()在程序结束时调用,但这很简单.

java multithreading shutdown executorservice

9
推荐指数
2
解决办法
8073
查看次数

Python Matplotlib:绘制线性不等式函数

如何使用matplotlib绘制由一些线性不等式函数限定的区域.

例如,如果我们有3个功能:Y <= -2 + 4X,Y> = 2 + 0.5×,Y <= 7 -0.3x

我想提请事端simmilar如Wolfram Alpha的作用:http://www3.wolframalpha.com/Calculate/MSP/MSP43251aca1dfd6ebcd862000067b9fd36a79h3igf?MSPStoreType=image/gif&s=39&w=200.&h=210.&cdf=Coordinates&cdf=Tooltips

python inequality function matplotlib

7
推荐指数
1
解决办法
8092
查看次数

Java - 调用方法会导致IllegalAccessError

我有一个简单的函数,用于从所选目录加载类"Myclass".

    // Variables
    File temp = new File("some path...");
    String class_name = "MyClass";

    // Directory url
    URL[] urls = null;
    try {
        urls = new URL[]{temp.toURI().toURL()};
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    // Loading the class
    ClassLoader cl = new URLClassLoader(urls);
    Class clazz = null;
    Object clazz_instance = null;
    try {
        // Loads class
        clazz = cl.loadClass(class_name);
        // Creates instance
        clazz_instance = clazz.newInstance();

        // Invoking method "myMethod"
        try {
            Method m = clazz.getMethod("myMethod");
            m.invoke(clazz_instance);
        } catch (NoSuchMethodException | InvocationTargetException …
Run Code Online (Sandbox Code Playgroud)

java

6
推荐指数
1
解决办法
285
查看次数