0

我正在尝试学习 Java 中的多线程概念。我在执行我的代码片段时遇到了错误。

我的代码片段:

class ThreadDemo {
public static void main(String args[]) {
   try{

   int [] arr = new int[10] ;
      for(int i = 0 ; i < 10 ; i++)
         arr[i] = i ;

   for(int c =0 ; c < 2 ; c++){
         for(int i = 0 ; i < 3 ; i++) //I want to run it on one thread
             System.out.println(arr[i]);
         for(int j = 0 ; j < 5 ; j++)  //I want to run it on another thread
             System.out.println(arr[j]);
            }

} catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
    }
   }
}

现在,为了解决这个问题,我尝试过,

class ThreadDemo {
public static void main(String args[]) {
try{

int [] arr = new int[10] ;
for(int i = 0 ; i < 10 ; i++)
arr[i] = i ;

    for(int c =0 ; c < 2 ; c++){
         Thread thread1 = new Thread () {
         public void run () {
         for(int i = 0 ; i < 3 ; i++) //I want to run it on one thread
           System.out.println(arr[i]);}
          };

          Thread thread2 = new Thread () {
          public void run () {
          for(int j = 0 ; j < 5 ; j++) //I want to run it on one thread
           System.out.println(arr[j]);}
          };


         }

} catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
    }
   }
}

但给出错误。谁能帮我解决这个问题?

4

2 回答 2

2

In

for (int c = 0; c < 2; c++) {
    Thread thread1 = new Thread() {//<- here

you are creating anonymous inner class that extends Thread class. You must know, that anonymous inner classes have access only to final local variables of method that they are created so if you want to gain access to int [] arr you must make it final like

final int[] arr = new int[10];

Also you created threads but you didn't start them. To do that invoke their start() method like thread1.start().


If you don't want to declare local variables of method as final you should consider creating threads not as anonymous inner classes but as separate classes for example

class MyThread extends Thread {
    int[] array;
    int iterations;

    public MyThread(int[] arr, int i) {
        array=arr;
        iterations = i;
    }

    @Override
    public void run() {
        for (int i = 0; i < iterations; i++)
            System.out.println(array[i]);
    }
}

class ThreadDemo {
    public static void main(String args[]) {
        try {

            int[] arr = new int[10];
            for (int i = 0; i < 10; i++)
                arr[i] = i;

            for (int c = 0; c < 2; c++) {
                MyThread thread1 = new MyThread(arr, 3);
                MyThread thread2 = new MyThread(arr, 5);
                thread1.start();
                thread2.start();
            }

        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
}
于 2012-07-30T00:01:54.487 回答
0

仅供参考:如果您实际上没有为其提供任何额外的逻辑,那么扩展 Thread 并不是最好的主意。最好使用 Runnable 并将其传递给线程构造函数。

Thread t = new Thread(new Runnable() {
    public void run() {
    }
});
t.start();

除此之外,正如其他人所指出的,直接在匿名类中的任何变量都需要声明为 final。

于 2012-07-30T04:57:08.587 回答