简体   繁体   English

我的代码未打印出来但可以正常工作

[英]My Code is not Printing out but working

public class MyThread{

    public MyThread(int m) {
        super();
    }

    public void run() {
        for (int x = 0; x < 201; x++) { 
            System.out.println("Thread Running" + x );       
        } 
    }

    public static void main(String[] args) {
        MyThread mt = new MyThread(200);
    }
}

The code runs, but it is not printing out. 该代码运行,但没有打印出来。 It is probably the constructor but the teacher told me to only make a public void run and a main method to allow the code to work. 它可能是构造函数,但老师告诉我只能进行一次公共无效运行,以及一种允许代码正常工作的主要方法。

Also let me know if I'm asking the wrong type of question, I tried looking at the 'How to ask questions' for better question asking. 如果我问的问题类型错误,也请告诉我,我尝试查看“如何问问题”以寻求更好的问题。

Looks like you are missing the implements Runnable for MyThread. 好像您缺少MyThread的implements Runnable实现。 And then you need to start() your mt thread. 然后,您需要start() mt线程。

You have two options: 您有两种选择:

  1. Extend Thread . 扩展Thread

     public class MyThread extends Thread{ 

    Then call start() in main . 然后在main调用start()

     mt.start(); 
  2. Implement Runnable . 实现Runnable

     public class MyThread implements Runnable{ 

    Then start a new Thread passing in your object, and call start() . 然后启动一个新的Thread传入您的对象,并调用start()

     Thread t = new Thread(mt); t.start(); 

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

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