从java中的线程池运行静态方法

发布于 2024-10-04 02:47:58 字数 312 浏览 4 评论 0原文

使用线程池在多个线程中运行静态方法的最佳方法是什么? 我还尝试将参数传递给静态方法。类似于

Class A{
 public static runTask(int i){
   ....
 }

}

和 来自主要内容:

ThreadPool pool = new ThreadPool(5, "poolname");

for(int i=1; i<10; i++){

   A.runTask(i) // but on a new thread...

}

谢谢!

What is the best way to run a static method in several threads, using a thread pool?
Also I trying to pass an argument to the static method. something like

Class A{
 public static runTask(int i){
   ....
 }

}

and from a main:

ThreadPool pool = new ThreadPool(5, "poolname");

for(int i=1; i<10; i++){

   A.runTask(i) // but on a new thread...

}

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陪我终i 2024-10-11 02:47:58

查看 java.util.concurrent.Executors。它应该满足您的需求。这是一个使用它的简单示例:

public class ExecutorServiceTest {
    static ExecutorService threadPool = Executors.newCachedThreadPool();

    public static void main(String[] args) throws Exception {
        // Queue 10 executions of someTask into the threadPool 
        for(int i = 0; i < 10; i++) {
            runSomeTaskInThreadPool();
        }
        // the shutdown method causes the executor to:
        // 1. stop accepting new tasks, and
        // 2. allow previously queued jobs to complete, and
        // 3. shut down all pooled threads once all jobs are complete  
        threadPool.shutdown();
        // block until the threadPool has finished shutting down,
        // which indicates that all tasks have finished executing
        threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
    }

    private static void runSomeTaskInThreadPool() {
        Future future = threadPool.submit(new Runnable() {
            public void run() {
                someTask();
            }
        });
        // TODO: Maybe keep track of futures to monitor success/failure of task
    }

    static AtomicInteger counter = new AtomicInteger();
    public static void someTask() {
        System.out.println("someTask: " + counter.incrementAndGet() 
                + " on thread: " + Thread.currentThread());
    }
}

Have a look at the documentation for java.util.concurrent.Executors. It should meet your needs. Here is a simple example of using it:

public class ExecutorServiceTest {
    static ExecutorService threadPool = Executors.newCachedThreadPool();

    public static void main(String[] args) throws Exception {
        // Queue 10 executions of someTask into the threadPool 
        for(int i = 0; i < 10; i++) {
            runSomeTaskInThreadPool();
        }
        // the shutdown method causes the executor to:
        // 1. stop accepting new tasks, and
        // 2. allow previously queued jobs to complete, and
        // 3. shut down all pooled threads once all jobs are complete  
        threadPool.shutdown();
        // block until the threadPool has finished shutting down,
        // which indicates that all tasks have finished executing
        threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
    }

    private static void runSomeTaskInThreadPool() {
        Future future = threadPool.submit(new Runnable() {
            public void run() {
                someTask();
            }
        });
        // TODO: Maybe keep track of futures to monitor success/failure of task
    }

    static AtomicInteger counter = new AtomicInteger();
    public static void someTask() {
        System.out.println("someTask: " + counter.incrementAndGet() 
                + " on thread: " + Thread.currentThread());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文