public class Chicks {
synchronized void yacks(long id)
{
for(int x = 1; x<3; x++)
{
System.out.println(id + " ");
Thread.yield();
}
}
}
class ChickYacks implements Runnable
{
Chicks c; // No exception if I declare it as static
public static void main(String[] args) {
new ChickYacks().go();
}
public void run()
{
c.yacks(Thread.currentThread().getId()); //Throws a Nullpointer exceptin
}
void go()
{
c = new Chicks();
new Thread(new ChickYacks()).start();
new Thread(new ChickYacks()).start();
}
}
为什么它会在run method()
. 在我看来一切都很好。当我声明Chicks 'c'
为静态但我不明白为什么?