方法1、加static
static class mythread extends Thread方法2、将mythread类定义移动到TestTongbu类外部
public class TestTongbu{
//...
}
class mythread extends Thread{}
static class mythread extends Thread方法2、将mythread类定义移动到TestTongbu类外部
public class TestTongbu{
//...
}
class mythread extends Thread{}
public static int i = 0; public static void main(String[] args) {
// static修饰的方法或代码块中不能引用具体的对象,this指当前对象,所以不能使用this
TestTongbu testTongbu = new TestTongbu();
MyThread t1 = testTongbu.new MyThread("t1");
MyThread t2 = testTongbu.new MyThread("t2");
t1.start();
t2.start();
} class MyThread extends Thread {
MyThread(String s) {
super(s);
} public void run() {
while(true) {
try {
i++;
Thread.sleep(1);
System.out.println(Thread.currentThread().getName()+i);
} catch (InterruptedException e) {
return;
}
}
}
}
}
你的代码实在是太烂了,类名要大写!内部类的new的方法是,使用外部类对象.new这样来new一个对象,你完全都不懂得。你得去看下内部类的相关知识。然后,或者也可以使用static的内部类,就不用像我代码中那样,使用外部类去new了。不知道你懂了没,欢迎继续提问。
然后还要在两个start之后加上
t1.join();
t2.join();
等线程执行完。
最后在print i。
可以的,i换成TestTongbu.i啊。
可以的,i换成TestTongbu.i啊。
对。它的i静态的。是可以。嘻嘻没注意。楼主结贴给分!!!