繁体   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);
    }
}

该代码运行,但没有打印出来。 它可能是构造函数,但老师告诉我只能进行一次公共无效运行,以及一种允许代码正常工作的主要方法。

如果我问的问题类型错误,也请告诉我,我尝试查看“如何问问题”以寻求更好的问题。

好像您缺少MyThread的implements Runnable实现。 然后,您需要start() mt线程。

您有两种选择:

  1. 扩展Thread

     public class MyThread extends Thread{ 

    然后在main调用start()

     mt.start(); 
  2. 实现Runnable

     public class MyThread implements Runnable{ 

    然后启动一个新的Thread传入您的对象,并调用start()

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

暂无
暂无

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

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