框里有若干鸡蛋,一个一个拿,拿完,2个2个拿剩一个,3个3个拿也剩一个,以此类推,直到7个7个拿正好不剩,问有多少鸡蛋...


public class EggTest {

/**
* @param args
*/
public static void main(String[] args) {

int eggnumber = 0;
for (int i = 0;; i++) {

if (i % 2 == 1 && i % 3 == 1 && i % 4 == 1 & i % 5 == 1 && i % 6 == 1 && i % 7 == 0) {

eggnumber++;
if (eggnumber >= 50 && eggnumber <= 100) {
System.err.println(i);
}

}
}

}

}



取中间的50个
在 Java 中,可以使用线程池来实现多个线程同时处理一组数的操作。具体步骤如下: 1. 创建一个线程池,可以使用 Java 自带的 Executors 工具类来创建线程池。 2. 把要处理的一组数分成若干份,每份由一个任务来处理。 3. 把这些任务提交到线程池中,线程池会自动创建若干个线程来处理这些任务。 4. 等待所有任务执行成,可以使用线程池的 awaitTermination 方法来等待。 示例代码如下: ```java import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Main { public static void main(String[] args) { int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int numThreads = 4; // 线程数 int size = nums.length / numThreads; // 每个任务处理的数的个数 ExecutorService pool = Executors.newFixedThreadPool(numThreads); for (int i = 0; i < numThreads; i++) { int startIndex = i * size; int endIndex = (i == numThreads - 1) ? nums.length : (i + 1) * size; Runnable task = new MyTask(nums, startIndex, endIndex); pool.execute(task); } pool.shutdown(); // 等待所有任务执行成 while (!pool.isTerminated()) {} System.out.println("All tasks are finished!"); } } class MyTask implements Runnable { private int[] nums; private int startIndex; private int endIndex; public MyTask(int[] nums, int startIndex, int endIndex) { this.nums = nums; this.startIndex = startIndex; this.endIndex = endIndex; } @Override public void run() { for (int i = startIndex; i < endIndex; i++) { // 处理 nums[i] System.out.println(Thread.currentThread().getName() + ": " + nums[i]); } } } ``` 上述代码中,创建了一个包含四个线程的线程池,把要处理的十个数分成四份,每个任务处理两到三个数。然后把这些任务提交到线程池中,等待所有任务执行成。每个任务的具体实现在 MyTask 类中实现,这里只是简单地输出任务要处理的数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值