2016-12-04 73 views
-1

在这个任务中,我应该有三个线程,这些线程应该从0开始计数,每秒打印一次计数并在7秒和15秒打印消息。多线程时出错

我找不出我的错误,我认为这是我的线程不知道他们指的是什么,但我不确定。

错误:

Exception in thread "Thread-0" Exception in thread "Thread-1" Exception in thread "Thread-2" java.lang.NullPointerException 
at TimePrinter.run(TimePrinter.java:7) 
at java.lang.Thread.run(Unknown Source) 

java.lang.NullPointerException 
at MessagePrinter2.run(MessagePrinter2.java:8) 
at java.lang.Thread.run(Unknown Source) 

java.lang.NullPointerException 
at MessagePrinter1.run(MessagePrinter1.java:8) 
at java.lang.Thread.run(Unknown Source) 


public class Counter{ 
    private int counter; 
    private Lock counterLock; 
    private Condition sevenCondition; 
    private Condition fifteenCondition; 
    public Counter() { 
     counter = 0; 
     counterLock = new ReentrantLock(); 
     sevenCondition = counterLock.newCondition(); 
     fifteenCondition = counterLock.newCondition(); 
    } 
    public void incrementCounter(){ 
     counterLock.lock(); 
     try{ 

      counter++; 
      if (counter%7 == 0) 
       sevenCondition.signal(); 
      //else sevenCondition.await(); 
      if (counter%15 == 0) 
       fifteenCondition.signal(); 
      //else fifteenCondition.await(); 
      System.out.print(" " + counter + " "); 

     } finally {counterLock.unlock();} 
    } 

    public void sevenSecondMessage()throws InterruptedException{ 
     counterLock.lock(); 
     try{ 
      while (counter%7 != 0){ 
       sevenCondition.await(); 
      System.out.println("Seven Second Message"); 
      } 
     } finally {counterLock.unlock();} 
    } 

    public void fifteenSecondMessage() throws InterruptedException{ 
     counterLock.lock(); 
     try{ 
      while (counter%15 != 0){ 
       fifteenCondition.await(); 
      System.out.println("Fifteen Second Message"); 
      } 
     } finally {counterLock.unlock();} 
    } 
} 


public class TimePrinter implements Runnable{ 
     private Counter counter; 
     public synchronized void run(){ 
      try { 
       counter.incrementCounter(); 

       Thread.sleep(1000); 
      } catch (InterruptedException e){} 
     } 
    } 



    public class MessagePrinter1 implements Runnable{ 
    private Counter counter; 
    public synchronized void run(){ 
     try { 

      counter.sevenSecondMessage(); 

      }catch (InterruptedException e){} 

     } 
    } 

    public class MessagePrinter2 implements Runnable{ 
     private Counter counter; 
     public synchronized void run(){ 
      try { 

       counter.fifteenSecondMessage(); 

       }catch (InterruptedException e){} 

      } 
     } 

    public class Main2 { 

    public static void main(String[] args){ 

     Runnable timeprinter = new TimePrinter(); 
     Runnable message1 = new MessagePrinter1(); 
     Runnable message2 = new MessagePrinter2(); 

     new Thread(timeprinter).start(); 
     new Thread(message1).start(); 
     new Thread(message2).start(); 
    } 
} 
+1

'计数器'不提及任何东西;解决该问题。 – ChiefTwoPencils

回答

1
public class TimePrinter implements Runnable{ 
    private Counter counter; 
    public synchronized void run(){ 
     try { 
      counter.incrementCounter(); 

      Thread.sleep(1000); 
     } catch (InterruptedException e){} 
    } 
} 

counter永远不会初始化。某处需要counter = new Counter()