Harish
Harish

Reputation: 7939

restricting execution time sample of threads in java

I want to simulate a scheduler in java. I have three threads defined. Now I want to execute Thread 1 to be take 10% time, Thread 2 to take 30% and Thread 3 to take remaining 60% of time approximately.

All the three threads are continous monitoring tasks which will never end.

i.e. If I execute the program for 100 minutes, then Thread 1 executes for 10 mins, Thread 2 for 30 mins & Thread 3 for 60 minutes.

and also whenever threads are being shifted (i.e. another threading going into running state), I should print that "Thread x executed for Y seconds"

Can any one please provide some pointers on achieving the above simulation in java.

Upvotes: 2

Views: 773

Answers (1)

fluminis
fluminis

Reputation: 4059

This link should be interresting.

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class MainThread
{
        public static void main(String[] args)
        {
                int corePoolSize = 2;
                ScheduledThreadPoolExecutor stpe = new ScheduledThreadPoolExecutor(corePoolSize);

                /*
                 * This will execute the WorkerThread immediately
                 */
                stpe.execute(new WorkerThread("WorkerThread-Running-Immediately"));

                /*
                 * This will execute the WorkerThread only once after 10 Seconds
                 */
                stpe.schedule(new WorkerThread("WorkerThread-Scheduled-After-10-seconds"), 10, TimeUnit.SECONDS);

                /*
                 * This will execute the WorkerThread continuously for every 5 seconds with an initial delay of 10
                 * seconds for the first WorkerThread to start execution cycle. In this case, whether the first
                 * WorkerThread is completed or not, the second WorkerThread will start exactly after 5 seconds hence
                 * called schedule at fixed rate. This continues till 'n' threads are executed.
                 */
                stpe.scheduleAtFixedRate(new WorkerThread("WorkerThread-Running-At-Fixed-Rate"), 10, 5, TimeUnit.SECONDS);

                /*
                 * This will execute the WorkerThread continuously with an initial delay of 10 seconds for the first
                 * WorkerThread to start execution cycle. Once the first thread execution completes then a delay of 5
                 * Seconds is introduced so that the next WorkerThread execution cycle starts. This continues till
                 * 'n' thread are executed. This is called schedule each thread with a fixed delay.
                 */
                stpe.scheduleWithFixedDelay(new WorkerThread("WorkerThread-Running-With-Fixed-Delay"), 10, 5, TimeUnit.SECONDS);
        }
}

And a worker thread :

public class WorkerThread implements Runnable
{
        private String  threadName      = null;

        public WorkerThread(String threadName)
        {
                this.threadName = threadName;
        }

        public void run()
        {
                System.out.println(this.threadName + " started...");
                try
                {
                        Thread.sleep(5000);
                }
                catch (InterruptedException e)
                {
                        e.printStackTrace();
                }
                System.out.println(this.threadName + " ended...");
        }
}

Upvotes: 2

Related Questions