限制java中线程的执行时间示例

发布于 2024-10-15 23:23:11 字数 289 浏览 2 评论 0原文

我想用java模拟一个调度程序。我定义了三个线程。现在我想执行线程 1 占用 10% 的时间,线程 2 占用 30% 的时间,线程 3 占用剩余 60% 的时间。

所有三个线程都是持续的监视任务,永远不会结束。

即,如果我执行程序 100 分钟,则线程 1 执行 10 分钟,线程 2 执行 30 分钟,线程 2 执行 30 分钟。线程 3 持续 60 分钟。

而且每当线程被转移时(即另一个线程进入运行状态),我应该打印“线程x执行Y秒”

任何人都可以提供一些关于在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.

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

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

发布评论

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

评论(1

洒一地阳光 2024-10-22 23:23:11

这个链接应该很有趣。

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);
        }
}

还有一个工作线程:

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...");
        }
}

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...");
        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文