几乎同时进行两个循环

发布于 2024-09-26 15:52:34 字数 458 浏览 0 评论 0原文

我不知道这个过程叫什么,但我发现这是可能的。 这个过程叫什么?

基本上,我有一个有循环的方法,并且在每次迭代中都有一个延迟秒。

function myLoop(float delay)
{
    for(int x=0; x<100; x++)
    {
        Print("Loop number: " + x);
        TimeDelay(delay);
    }
}

我想运行第二个实例而不等待第一个实例完成。

function main()
{
     myLoop(2);
     myLoop(2);
}

所以一旦第一个 myLoop 启动,我希望第二个 myLoop 立即启动,它们将同时运行,我的问题是,你怎么称呼这个过程? 这个过程可能吗?(例如在java中)。

非常感谢! :)

i don't what this process is called, but i've seen that it's possible.
what is this process called?

basically, i have a method that has a loop, and in every iteration has a delay second.

function myLoop(float delay)
{
    for(int x=0; x<100; x++)
    {
        Print("Loop number: " + x);
        TimeDelay(delay);
    }
}

i'd like to run the second instance without waiting until the first instance is finished.

function main()
{
     myLoop(2);
     myLoop(2);
}

so once the first myLoop started, i'd like the second myLoop to start immediately, they would be both running at the same time, my question is, what do you call this process?
is this process possible?(in java for example).

Thank you very much! :)

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

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

发布评论

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

评论(4

一场信仰旅途 2024-10-03 15:52:34

这通常需要某种形式的多线程

您可以执行以下操作:

function main
    start thread, calling myLoop(2)
    start thread, calling myLoop(2)

    ' Potentially wait for threads to complete
end function

有关 Java 中如何工作的详细信息,请参阅并发教程< /a>.

This typically requires some form of multithreading.

You would do something like:

function main
    start thread, calling myLoop(2)
    start thread, calling myLoop(2)

    ' Potentially wait for threads to complete
end function

For details on how this works in Java, see the Concurrency Tutorial.

冬天旳寂寞 2024-10-03 15:52:34

您的程序的 Java 实现将与此类似。

    class MyThread implements Runnable{
       Thread t;
       int delay;
       MyThread (int delay) {
          t = new Thread(this,"My thread");
          this.delay = delay;
          t.start();
       }
       public void run() {
          for(int x=0; x<100; x++)
          {
           Print("Loop number: " + x);
           TimeDelay(delay);
          }
      }
    }
    class Demo {
       public static void main (String args[]){
         Thread t1 = new MyThread(2);
         Thread t2 = new MyThread(2);
         t1.join();
         t2.join();    
       }
    }

Java implementation of your program will be similar to this.

    class MyThread implements Runnable{
       Thread t;
       int delay;
       MyThread (int delay) {
          t = new Thread(this,"My thread");
          this.delay = delay;
          t.start();
       }
       public void run() {
          for(int x=0; x<100; x++)
          {
           Print("Loop number: " + x);
           TimeDelay(delay);
          }
      }
    }
    class Demo {
       public static void main (String args[]){
         Thread t1 = new MyThread(2);
         Thread t2 = new MyThread(2);
         t1.join();
         t2.join();    
       }
    }
甜点 2024-10-03 15:52:34

您的问题的答案。

  1. 这叫什么?
    A:线程——同时运行多个任务。 (我们在 PHP/Linux 应用程序中将其称为分叉。)

  2. 这在 Java 中可能吗?
    答:当然这是可能的。坦率地说,这在 Java 中更容易实现。请遵循上述答案。

The answer for your questions.

  1. What is this called?
    A: Threading - running multiple tasks at a same time. (We call it as forking in PHP/Linux applications.)

  2. Is this possible in Java?
    A: Offcourse this is possible. To be frank this is more easier to implement in Java. Please follow the above answers.

梦醒灬来后我 2024-10-03 15:52:34

这称为异步计算。您可以使用 Futures 干净地解决这个问题。 (您实际上并不需要进行全面的多线程处理)

http://download.oracle.com/javase/6/docs/api/java/util/concurrent/Future.html

This is called an asynchronous computation. You can solve this cleanly with Futures. (You don't really need to do full-blown multithreading)

http://download.oracle.com/javase/6/docs/api/java/util/concurrent/Future.html

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