简体   繁体   English

我在Java服务器中拥有五个线程,如何使它们同时运行一个任务?

[英]I hava five threads in java server, how should I do to make them run one task concurrently?

I write a web server in java, and need a task class to upload data to anther server, so I do this as below: 我用Java编写了一个Web服务器,并且需要一个任务类将数据上传到另一个服务器,所以我这样做如下:

ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(5)
executor.scheduleAtFixedRate(new Task(), 0, 100, TimeUnit.SECOND);

The Task has implement Runnable . Task已实现Runnable The question is,every time only one thread is scheduled to run this new Task() , others just idle, if a large number of data coming, my work queue which store data become very large, more worse, I can't get real time data in anther server. 问题是,每次只有一个线程被调度运行此new Task() ,其他线程才处于空闲状态,如果有大量数据传入,则存储数据的工作队列变得非常大,更糟糕的是,我无法获得真实的花药服务器中的时间数据。 My English is too bad, I am wandering if you have my got my point, I want to find a way to fix this, can you help me ? 我的英语太糟糕了,如果您明白我的意思,我在徘徊,我想找到一种解决办法,您能帮我吗?

To run 5 tasks at once you need 5 tasks which take long enough that different threads will be used. 要一次运行5个任务,您需要5个任务,这些任务需要很长的时间才能使用不同的线程。 If you add one task, it will use one thread. 如果添加一个任务,它将使用一个线程。 If you add 5 small tasks, they might be all run by the same thread. 如果添加5个小任务,它们可能全部由同一线程运行。

Have you tried this? 你有尝试过吗?

ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(5);
Task task=new Task();
executor.scheduleAtFixedRate(task, 0, 100, TimeUnit.SECOND);
executor.scheduleAtFixedRate(task, 0, 100, TimeUnit.SECOND);
executor.scheduleAtFixedRate(task, 0, 100, TimeUnit.SECOND);
executor.scheduleAtFixedRate(task, 0, 100, TimeUnit.SECOND);
executor.scheduleAtFixedRate(task, 0, 100, TimeUnit.SECOND);

in this way you will have Only one task scheduled to run in 5 threads. 这样,您将只有一个任务计划在5个线程中运行。 just make sure you have Task thread safe (as you will be running same task within 5 threads). 只要确保您具有Task thread safe (因为您将在5个线程中运行同一任务)即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何同时运行线程? - How do I make concurrently running threads? 如何使客户端服务器Java应用程序在一个端口上发送消息,而在另一个端口上接收消息? - How do I make a client-server Java application to send messages on one port but receive them on another? 如何同时运行Java应用程序多次? - How do I run a Java application multiple times concurrently? 如何在Java中运行不同的线程? - How do I run different threads in Java? 我有两个线程如何知道它们是否同时运行? - I have two threads how do I know if they are running concurrently? 如何确保在 java 中一个线程始终先于其他线程运行? 给定两个线程。 我需要确保线程 1 始终领先于线程 2 - How do I ensure that one thread always run ahead of other in java? Given two threads. I need to make sure that Thread 1 always leads Thread 2 如何在一个子线程的某些子线程完成之前返回一个Java线程? - How do I make one Java thread return before some of its child threads finish? 如何使这两个线程同时运行? - How do I make these two threads run togeather? 我应该能够同时打开多少个 Java HttpURLConnections? - How Many Java HttpURLConnections Should I Be Able to Open Concurrently? 如何在Java的单独线程中运行客户端套接字? - How do I run my client socket in seperate threads in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM