public class UnsynchronizedCounterTest{
/**
* A class representing a counter with a method for incrementing the coutner. No synchronization is used
* so this counter is not thread safe
* @author
*
*/
static class Counter{
int count;
void inc() {
count = count+1;
}
int getCount() {
return count;
}
}
static Counter counter; // The counter that will be incremented. Since it is a global static variable, it is used by all threads of type HWUExercise12_1Thread. It is shared resource
static int numberOfIncrements;
static class IncrementerThread extends Thread{
public void run() {
for (int i=0; i < numberOfIncrements; i++) {
counter.inc();
}
}
}
/**
* The main program runs in a loop until the user want to exit. Each time through the loop, it runs one experiemtn.
* It gets the number of threads and the number of increments per thread from the user. It creates and starts the
* threads, and then waits for all to finsih. It prints the fibna lvalue of the counter, as well as the expected value.
* The program ends when the user enters a number less than or equal to zero as the number of threads.
* @param Args
*/
public static void main(String Args[]){
Counter counter = new Counter();
Scanner reader = new Scanner(System.in);
while(true) {
System.out.print("Enter the number of threads");
int numberOfThreads = Integer.parseInt(reader.nextLine());
if (numberOfThreads < 0) {
break;
}do {
System.out.print("Enter the number of increments per thread");
int numberOfIncrements = reader.nextInt();
//create thread array
IncrementerThread [] threads = new IncrementerThread[numberOfThreads];
//Create a thread for each position in array
for (int i=0; i< threads.length; i++) {
threads[i] = new IncrementerThread();
}
}while (numberOfIncrements <= 0);
//start the threads
for (int i=0; i < numberOfThreads; i++) {
threads[i].start();
}
try {
for(int j=0; j< numberOfThreads; j++) {
threads[j].join();
}
System.out.println("The finanl value of the counter is" + counter.getCount());
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
For this program, I created a thread class (not thread-safe) that calls the inc () method in the nested class, a specified number of times. My program is supposed to create several threads and start them all and then wait for all the threads to terminate. I also have to print the final value of the counter. My error is that the eclipse is saying the the thread array I created, threads cannot be resolved to a variable. It's giving me an error for the lines
threads[i].start();
threads[j].join();
I'm a beginner in java and I'm not sure how to fix this issue. My question is that why is it saying threads cannot be resolved to a variable when I clearly have a thread object array? Thanks.
答案 0 :(得分:-2)
这是一个有效的代码:
public class UnsynchronizedCounterTest{
static Counter counter;
public static void main(String Args[]){
new UnsynchronizedCounterTest().startCounting();
}
public void startCounting() {
counter = new Counter();
Scanner reader = new Scanner(System.in);
while(true) {
System.out.print("Enter the number of threads: ");
int numberOfThreads = reader.nextInt();
if (numberOfThreads <= 0) {
break;
}
IncrementerThread [] threads;
do {
System.out.print("Enter the number of increments per thread");
numberOfIncrements = reader.nextInt();
//create thread array
threads = new IncrementerThread[numberOfThreads];
//Create a thread for each position in array
for (int i=0; i< threads.length; i++) {
threads[i] = new IncrementerThread();
}
}while (numberOfIncrements <= 0);
//start the threads
for (int i=0; i < numberOfThreads; i++) {
threads[i].start();
}
try {
for(int j=0; j< numberOfThreads; j++) {
threads[j].join();
}
System.out.println("The finanl value of the counter is: " + counter.getCount());
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
class IncrementerThread extends Thread{
@Override
public void run() {
for (int i=0; i < numberOfIncrements; i++) {
counter.inc();
}
}
}
class Counter{
int count = 0;
void inc() {
count = count+1;
}
int getCount() {
return count;
}
}
}