[英]Java thread: start and sleep threads at random time
i am trying to make a server-client Socket program (TCP) in java. 我试图用Java编写服务器-客户端套接字程序(TCP)。 In my client program, i have created 10 threads, these 10 threads will act as a separate client and when it runs it connnects to socket at server side.
在我的客户端程序中,我创建了10个线程,这10个线程将充当一个单独的客户端,并且在运行时,它将连接到服务器端的套接字。
Now, I want that the threads should start at random time then go to sleep at random time and then again resume from their sleep state. 现在,我希望线程应该在随机时间启动,然后在随机时间进入睡眠状态,然后再次从其睡眠状态恢复。 this randomization is because i am running client and server program on localhost, so i want that threads should behave as there are many users who access a single server(like we have google) at any instant of time.
这种随机化是因为我正在本地主机上运行客户端和服务器程序,所以我希望线程的行为应该像在任何时刻都可以访问许多服务器的用户一样(例如我们有google)。
I am not any solution for this... plz help me and suggest something which i can try. 我对此没有任何解决方案...请帮助我,并提出一些我可以尝试的建议。
I have tried Timer and TimerTask class... but it is not fulfilling my need.. Timer class perform its assigned tasks in a sequence.. not in random manner.. 我已经尝试过Timer和TimerTask类...但是它不能满足我的需要.. Timer类按顺序执行其分配的任务..不是以随机的方式..
so is there any solution instead of Timer and TimerTask . 所以有什么解决方案,而不是Timer和TimerTask 。
You can use a scheduled executor with fixed thread pool size, that sleeps at random time and initially starts at random time: 您可以使用具有固定线程池大小的调度执行程序 ,该执行程序在随机时间休眠,最初在随机时间启动:
import java.util.Random;
import java.util.concurrent.*;
public class Client{
private final static ScheduledExecutorService executor = Executors.newScheduledThreadPool(10);
public static void main(String[] args) {
final Random random = new Random();
final long maxSleepTime=2000L;
for (int i = 0; i < 10; i++) {
int randomSleepTime = random.nextInt((int) maxSleepTime);
Runnable command=new Runnable() {
@Override public void run() {
//code to run, e.g. call a server method
}
};
executor.scheduleAtFixedRate(command,randomSleepTime,randomSleepTime, TimeUnit.MILLISECONDS);
}
}
}
this example use an executor with 10 threads... the idea is to submit your tasks and the executor will handle the threads for you. 本示例使用具有10个线程的执行程序...这个想法是提交您的任务,执行程序将为您处理线程。
public class Test {
private final Executor executor= Executors.newFixedThreadPool(10);
public void executeTask(Runnable task){
executor.execute(task);
}
public static void main(String[] args) {
Test test= new Test();
for(int i=0;i<100;i++){
final int iteration=i;
Runnable task=(new Runnable() { public void run() {System.out.println("Executing task "+iteration); }});
test.executeTask(task);
}
}
} }
A very straight forward solution could look like this: 一个非常简单的解决方案可能是这样的:
Call Thread.sleep(sleepTime);
调用
Thread.sleep(sleepTime);
whereas sleepTime
is generated randomly. 而
sleepTime
是随机生成的。
import java.util.Random;
public class Client extends Thread
{
boolean run = true;
public void run() {
Random rnd = new Random();
try {
while(run == true) {
// ..
// Do whatever a client does ..
// ..
Integer sleepTime = rnd.nextInt(100);
Thread.sleep(sleepTime);
System.out.println("Slept for " + sleep + "ms");
// Replace this by an appropriate condition
if(sleep > 50) {
System.out.println("I'm out ..");
run = false;
}
}
} catch(InterruptedException v) {
System.out.println(v);
}
}
}
public class Main{
public static void main(String []args){
Client client = new Client();
client.run();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.