multithreading - Java thread: start and sleep threads at random time -


i trying make server-client socket program (tcp) in java. in client program, have created 10 threads, these 10 threads act separate client , when runs connnects socket @ server side.

now, want threads should start @ random time go sleep @ random time , again resume sleep state. randomization because running client , server program on localhost, want threads should behave there many users access single server(like have google) @ instant of time.

i not solution this... plz me , suggest can try.

i have tried timer , timertask class... not fulfilling need.. timer class perform assigned tasks in sequence.. not in random manner..

so there solution instead of timer , timertask.

you can use scheduled executor fixed thread pool size, sleeps @ random time , starts @ 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;         (int = 0; < 10; i++) {             int randomsleeptime = random.nextint((int) maxsleeptime);             runnable command=new runnable() {                 @override public void run() {                     //code run, e.g. call server method                 }             };             executor.scheduleatfixedrate(command,randomsleeptime,randomsleeptime, timeunit.milliseconds);         }     } } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -