简体   繁体   English

为什么不能使用类的实例创建新的线程?

[英]Why can't I create a new Thread with an instance of my class?

I wanted to know, why following code does not work: 我想知道为什么以下代码不起作用:

public class ClockRunnable extends Applet implements Runnable {

Thread m_zeit;
Thread m_background;

 public void init() {
    m_zeit = new Thread(new ClockRunnable());
    m_background = new Thread(new Background());

    m_zeit.start();
    m_background.start();

  }
}

This is of course only an example code. 当然,这只是示例代码。 I was wondering, because if I create a new Background Thread it works like written above. 我在想,因为如果我创建一个新的后台线程,它的工作方式就像上面写的那样。 But if I call a new Thread for my ClockRunnable class, it throws me a NullPointerExecption. 但是,如果我为ClockRunnable类调用新的线程,则会抛出NullPointerExecption。 But if I change the new Thread command to 但是如果我将新的Thread命令更改为

        m_zeit = new Thread(this);

It works perfectly. 它运作完美。

If the example above is not enough, here is the whole code + ErrorOutput: 如果上面的示例还不够,那么这是完整的代码+ ErrorOutput:

import java.util.*;
import java.text.*;
import java.awt.*;
import java.applet.*;

public class UhrzeitRunnable extends Applet implements Runnable {
String m_aktZeit;
DateFormat m_formatierer;
Font m_anzeigeFont;
Color m_farbe;
Thread m_zeit;
Thread m_background;

public void init() {
    m_anzeigeFont = new Font("Serif",Font.BOLD,22);
    m_formatierer = DateFormat.getTimeInstance();
    m_aktZeit = m_formatierer.format(new Date());
    m_zeit = new Thread(new UhrzeitRunnable());   // if I change it to "this" it works
    m_background = new Thread(new Background());
    m_zeit.start();
    m_background.start();

}

public void run() {
    while(true) {
        m_aktZeit = m_formatierer.format(new Date());  //NullPointerExeption on this line
        repaint();
        try {
            Thread.sleep(1000); 
        } catch (InterruptedException e) {
            return;
        }
    }
}

public void start() {
    if(m_zeit == null) {
        m_zeit = new Thread(new UhrzeitRunnable());
        m_zeit.start();
    }
    if (m_background==null) {
        m_background = new Thread(new Background());
        m_background.start();
    }
}

public void stop() {
    if(m_zeit!=null){
        m_zeit.interrupt();
        m_zeit = null;
    }
}

public void destroy() {
    if(m_zeit !=null) {
        m_zeit.interrupt();
        m_zeit = null;
    }
}

public void paint(Graphics g) {
    g.setFont(m_anzeigeFont);
    g.setColor(Color.blue);
    this.setBackground(m_farbe);
    g.drawString(m_aktZeit, 20, 45);
}


public class Background implements Runnable {
public void run() {
    while (true) {
        m_farbe = new Color((int) (255*Math.random()),(int) (255*Math.random()), (int)(255*Math.random()));
            repaint();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {

        }
    }
}
}
}

And the Error output. 和错误输出。 The background thread works perfectly by the way. 后台线程可以完美地工作。

Exception in thread "Thread-3" java.lang.NullPointerException
at kapitel15.UhrzeitRunnable.run(UhrzeitRunnable.java:29)
at java.lang.Thread.run(Unknown Source)

Applets are strange beasts, and it is very unusual for you to call a constructor on one. 小程序是奇怪的野兽,您在其中调用构造函数是非常不寻常的。 Usually this is done by the web page's Java applet driver which then calls the the Applet's init method. 通常,这是通过网页的Java applet驱动程序完成的,然后再调用Applet的init方法。 And when you do call the constructor, the init() is not called, leaving some of your key fields null. 当您调用构造函数时,不会调用init(),从而使某些键字段为空。 But why would you want to do this? 但是为什么要这么做呢? The Applet that you create is a completely distinct object from the one that is being displayed, and changes in its state will not be reflected in the displayed object. 您创建的Applet是与正在显示的Applet完全不同的对象,并且其状态更改不会反映在显示的对象中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM