简体   繁体   English

Java中的for循环如何让多个线程同时运行?

[英]How to make multiple Threads run at the same time using a for loop in Java?

I'm pretty new to java and started building a small program to find prime numbers in a given range.我对 java 很陌生,并开始构建一个小程序来查找给定范围内的素数。 Now I tried to implement multitasking, eg splitting the numbers to be checked into n parts that shall be checked independently.现在我尝试实现多任务处理,例如将要检查的数字分成应独立检查的 n 个部分。 I wanted to make it as flexible as possible, so I didn't specify the number of threads in the code, instead I used a prompt variable.我想让它尽可能灵活,所以我没有在代码中指定线程数,而是使用了一个提示变量。 Now, the code works, but the threads don't run parallel.现在,代码可以工作,但线程不能并行运行。

What seems to be the problem here?这里似乎有什么问题?

           [...]     
           System.out.println("How many Threads shall be used?");
           int b1 = scan.nextInt();
           scan.close();
           for (int i = 1; i <= b1; i++) {
             long u = (q/b1)*(i-1);
             long o = (q/b1)*i;
             System.out.println("Thread "+i+" started.");
             Thread t1 = new Thread(new thread1 (u, o, i));
             t1.start();
           }


class thread1 implements Runnable{
    public thread1 (long a, long b, int c) {
//a= min. number, b = max. number, c=number of Thread
        primefinder.findPrimes(b,a, c);
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
    }
}

From the javadochttps://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html :从 javadochttps://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html

class PrimeRun implements Runnable {
         long minPrime;
         PrimeRun(long minPrime) {
             this.minPrime = minPrime;
         }

         public void run() {
             // compute primes larger than minPrime
              . . .
         }
     }
    

Your logic must be in the run() method.您的逻辑必须在 run() 方法中。 It's this method that will be called and run on another thread.正是这个方法将被调用并在另一个线程上运行。 With your actual code, you do the calculation in the Runnable's constructor, which is called in your for loop, and therefor is sequential.使用您的实际代码,您可以在 Runnable 的构造函数中进行计算,该构造函数在您的 for 循环中调用,因此是顺序的。

Moving your calculation to the run method will solve the problem.将您的计算移至运行方法将解决问题。

As the correct Answer by Daniel says, you need to move your business logic into the run method of a Runnable , or the call method of a Callable .正如Daniel 的正确答案所说,您需要将业务逻辑移动到Runnablerun方法或Callablecall方法中。

Also, in modern Java we rarely address the Thread class directly.此外,在现代 Java 中,我们很少直接处理Thread class。 Instead use the Executors framework.而是使用 Executors 框架。

ExecutorService es = Executors.newFixedThreadPool( x ) ;
for( … ) { es.submit( task ) ; }
es.shutdown() ;
es.awaitTermination( … ) ;

This has been covered many times already on Stack Overflow.这已经在 Stack Overflow 上讨论过很多次了。 So search to learn more.因此,搜索以了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在java中同时运行两个线程 - How to run two threads at the same time in java 如何在Java中同时运行线程和访问相同资源 - How to run threads at the same time and access same resource in java 如何使用java中的循环创建多个线程 - How to create multiple threads using a loop in java 多线程在java中使用同一个对象会复制它吗? - Do multiple threads Using the same object in java make a copy of it? 如何在 Java 中运行同一进程的多个线程 - Spring Boot - How to run multiple threads of the same process in Java - Spring Boot Java Multithreading使线程以与启动时相同的顺序结束,但同时运行 - Java Multithreading make threads end in same order they started but run at same time 如何使用for循环一次运行多个登录? - How to run multiple logins at a time with using for loop? 如何在Java(Selenium)设置中使用Apache POI多个线程(并行测试用例)同时访问相同的excel文件? - How can multiple threads (parallel testcases) access same excel file at same time using Apache POI in Java (Selenium) setup? 多个线程如何通过使用Java中的同一连接来写入数据库? - How can multiple Threads write to the database by using the same connection in java? 如何使两个线程同时工作 - How to make two threads work in same time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM