1、实现3个类:Storage、Counter和Printer。Storage存储整数,Counter创建线程,线程从0
开始计数(0,1,2…),并将每个值存储到Storage中。Printer类创建一个线程读取并打印Storage
中的值,并确保每个值只打印一次。我是这么做得
class Storage {
    Thread[] ary = new Thread[10];
}class Counter extends Thread{
Storage st;
Counter() {
new Thread(this).start();
}
public void run() {
for(int i=0; i<10; i++) {
st.ary[i] =new Thread("thread" +i);
}
}
}class Printer extends Thread{
  Storage st;
  Printer() {
   new Thread(this).start();
  }
public void run() {
for(int i=0; i<10; i++) {
System.out.println(st.ary[i]);
}
}public class Test {
public static void main(String[] args) {
Counter c = new Counter();
Printer p = new Printer();

}}为什么就有编译错误呢?错误如下
F:\java\Thread\cps2>java Test
Exception in thread "Thread-1" java.lang.NullPointerException
        at Counter.run(Counter.java:10)
        at java.lang.Thread.run(Thread.java:595)
Exception in thread "Thread-3" java.lang.NullPointerException
        at Printer.run(Printer.java:8)
        at java.lang.Thread.run(Thread.java:595)

解决方案 »

  1.   

    存在空引用,使用了未初始化的对象。
    改下:
    class Storage {
       static Thread[] ary=new Thread[10];
    }
    要不然你的Couter和printer怎么能共享数据呢。另外你的printer的run后面少个"}"。
      

  2.   

    class Storage { 
        Thread[] ary = new Thread[10]; 
    } class Counter extends Thread{ 
    Storage st = new Storage();
    Counter() { 
    new Thread(this).start(); 

    public void run() { 
    for(int i=0; i <10; i++) { 
    st.ary[i] =new Thread("thread" +i); 
    System.out.println(st.ary[i]);


    } class Printer extends Thread{ 
      //Storage st; 
      Printer() { 
      new Thread(this).start(); 
      } 
    public void run() { 
    for(int i=0; i <10; i++) { 
    System.out.println(i);//st.ary[i]); 


    }public class Test { 
    public static void main(String[] args) { 
    Counter c = new Counter(); 
    Printer p = new Printer(); } } 
    /*
    刚初始化过,并未能解决.于是想了想.
    改了一下代码给楼主看.相信你会明白什么原因了.
    */
      

  3.   

    如果这么做呢
    public class Storage {
        int value;    public  void setValue(int i) {
            value = i;
        }    public  int getValue() {
            return value;
        }}
    public class Counter implements Runnable {
        Storage storage;    Counter(Storage target) {
            storage = target;
            new Thread(this).start();
        }    public void run() {
            int i = 0;
            while (true) {
                storage.setValue(i);
                i++;
            }
        }    public static void main(String[] args) {
            Storage store = new Storage();
            new Counter(store);
            new Printer(store);
        }
    }
    public class Printer implements Runnable {
        Storage storage;
        Printer(Storage source) {
            storage = source;
            new Thread(this).start();
        }    public void run() {
            while(true) {
                System.out.println(storage.getValue());
            }
        }}
    如果这样写怎么有的时候打印出负数,而且不是我要的从0开始的啊?
      

  4.   

    class Storage

    Thread[] ary = new Thread[10]; 

    class Counter extends Thread

    Storage st = new Storage(); 
    public void run()

    for(int i=0; i <10; i++)

    st.ary[i] =new Thread("thread" +i); 
    System.out.println(st.ary[i].getName()); 



    class Printer extends Thread

    public void run()

    for(int i=0; i <10; i++)

    System.out.println(i);


    } public class Test11

    public static void main(String[] args)

    Counter c = new Counter(); 
    c.start();
    Printer p = new Printer(); 
    p.start();
    } } 
      

  5.   

    class Storage 

    Thread[] ary = new Thread[10]; 

    class Counter extends Thread 

    Storage st = new Storage(); 
    public void run() 

    for(int i=0; i <10; i++) 

    st.ary[i] =new Thread("thread" +i); 
    System.out.println(st.ary[i].getName()); 



    class Printer extends Thread 

    public void run() 

    for(int i=0; i <10; i++) 

    System.out.println(i); 


    } public class Test11 

    public static void main(String[] args) 

    Counter c = new Counter(); 
    c.start(); 
    Printer p = new Printer(); 
    p.start(); 
    } } 
      

  6.   

    要考虑同步问题,synchronized
    public class Storage { 
        int value;     public  void setValue(int i) { 
            value = i; 
        }     public  int getValue() { 
            return value; 
        } } 
    public class Counter implements Runnable { 
        Storage storage;     Counter(Storage target) { 
            this.storage = target; 
        }     public synchronized void run() { 
            int i = 0; 
            while (true) { 
                storage.setValue(i); 
                i++; 
            } 
        }     public static void main(String[] args) { 
            Storage store = new Storage(); 
            Thread t1 = new Counter(store); 
            Thread t2 = new Printer(store);
            t1.start();
            t2.start();
        } 

    public class Printer implements Runnable { 
        Storage storage; 
        Printer(Storage source) { 
            storage = source; 
        }     public synchronized void run() { 
            while(true) { 
                System.out.println(storage.getValue()); 
            } 
        } } 
    并不能保证数一个数就能打印一个数,就像生产者和消费者问题,要考虑线程通信问题
      

  7.   

    class Storage { 
        Thread[] ary = new Thread[10]; 

    很显然,你只是给ary分配了空间。你并未对ary进行初始化.
    应该加上for (Thread thread : ary) {
      thread = new Thread() {
        public void run() {
          ....
        }
      }
    }
      

  8.   

    class Storage { 
        static Thread[] ary = new Thread[10]; 
    } class Counter extends Thread{ 
    Storage st=new Storage(); 
    Counter() { 
    new Thread(this).start(); 

    public void run() { 
    for(int i=0; i <10; i++) { 
    st.ary[i] =new Thread("thread" +i); 


    } class Printer extends Thread{ 
      Storage st; 
      Printer() { 
      new Thread(this).start(); 
      } 
    public void run() { 
    for(int i=0; i <10; i++) { 
    System.out.println(st.ary[i]); 

    } public class Test { 
    public static void main(String[] args) { 
    Counter c = new Counter(); 
    Printer p = new Printer(); } }