我刚刚购买了新的Arduino 1010,但似乎无法打印为串行。波特率是相同的,并且我已经更新了所有固件。有什么建议么?
在较长的程序没有将任何内容打印到串行并且它们都没有打印到串行之后,我尝试仅运行这样的测试程序。
import java.util.*;
import java.util.concurrent.*;
public class SumPrimes {
private boolean array[];
private int numberOfWorkers;
private Semaphore allFinished;
public SumPrimes(int num, int threads){
array = new boolean[num];
numberOfWorkers = threads;
for (int i = 2; i < num; i++)
array[i] = true;
}
private class SumParallel extends Thread {
int min;
int max;
long sum;
SumParallel(int min, int max){
this.min = min;
this.max = max;
sum = 0;
}
public void run() {
for (int i = min; i < max; i++) {
if (array[i]) {
for (int j = min; j*i < array.length; j++) {
array[i*j] = false;
}
sum += i;
}
}
allFinished.release();
}
public long getSum() {
return sum;
}
}
public void SumInParallel() {
allFinished = new Semaphore(0);
List<SumParallel> workers = new ArrayList<SumParallel>();
int lengthOfOneWorker = array.length / numberOfWorkers;
for (int i = 0; i < numberOfWorkers; i++) {
int start = i * lengthOfOneWorker;
int end = (i+1) * lengthOfOneWorker;
if (i == numberOfWorkers - 1)
end = array.length;
SumParallel worker = new SumParallel(start, end);
workers.add(worker);
worker.start();
}
try {
allFinished.acquire(numberOfWorkers);
} catch (InterruptedException ignored) {}
int sum = 0;
for (SumParallel w : workers){
sum += w.getSum();
}
System.out.println("The sum of prime numbers is: " + sum);
}
public static void main(String[] args) {
int limitNum = Integer.parseInt(args[0]);
int threadNum = Integer.parseInt(args[1]);
SumPrimes sum_primes = new SumPrimes(limitNum, threadNum);
sum_primes.SumInParallel();
}
}