我的代码如下:
package test;
public class PrintNumber implements Runnable {
int num = 1;
@Override
public void run() {
synchronized (this) {
while (num <= 100) {
notify();
try {
Thread.currentThread().sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ": " + num);
num++;
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
class TestThread {
public static void main(String[] args) {
PrintNumber h = new PrintNumber();
Thread t1 = new Thread(h);
Thread t2 = new Thread(h);
t1.setName("华东交通大学");
t2.setName("九江学院");
t1.start();
t2.start();
}
}
点击run as却没有找到Java application。查阅其他人给出的意见,却被告知缺少主函数,可是明显有public static void main(String[] args).
那是什么问题呢?
我尝试很久,终于发现我的主函数实现类,并不是文件类。即我是最先新建
PrintNumber类
而不是
TestThread类
所以将两个类名字互换即可达到效果。