这是整个代码:
import java.util.ArrayList;
public class Test<E extends Comparable<E>>{
ThreadLocal<ArrayList<E>>arraylist=new ThreadLocal<ArrayList<E>>(){
@Override
protected ArrayList<E> initialValue() {
// TODO Auto-generated method stub
//return super.initialValue();
ArrayList<E>arraylist=new ArrayList<E>();
for(int i=0;i<=20;i++)
arraylist.add((E) new Integer(i));
return arraylist;
}
};
class MyRunnable implements Runnable{
private Test mytest;
public MyRunnable(Test test){
mytest=test;
// TODO Auto-generated constructor stub
}
@Override
public void run() {
System.out.println("before"+mytest.arraylist.toString());
ArrayList<E>myarraylist=(ArrayList<E>) mytest.arraylist.get();
myarraylist.add((E) new Double(Math.random()));
mytest.arraylist.set(myarraylist);
System.out.println("after"+mytest.arraylist.toString());
}
// TODO Auto-generated method stub
}
public static void main(String[] args){
Test test=new Test<Double>();
System.out.println(test.arraylist.toString());
new Thread(new MyRunnable(test)).start();
new Thread(new MyRunnable(test)).start();
System.out.println(arraylist.toString());
}
}
我的问题是:
- 为什么
new Thread(new MyRunnable(test)).start();
会导致错误:
Cannot make a static reference to the non-static type MyRunnable
? - 术语“静态引用”指的是什么?