菜鸟一枚,路过的大虾帮忙看一下
package xiancheng;public class Testsyn implements Runnable(){//为什么实现一个接口他还报错呀
Timer timer = new Timer();//这个地方它显示的是分号错了
public static void main(String[] args) {
// TODO Auto-generated method stub
Testsyn t = new Testsyn();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
timer.add(Thread.currentThread().getName());
}
}
class Timer{
private static int num = 0;
public void add(String name){
num++;
try{
Thread.sleep(1);
}catch(InterruptedException e){
System.out.println(name +"是第"+"num"+"个访问者");
}
}
}
----------------解决方案--------------------------------------------------------
语法错误
----------------解决方案--------------------------------------------------------
你这是写一个进程,你见过在进程中写main方法的嘛???,写一个测试类,来掉用main方法,main方法也是一个进程,进程中调用进程???
----------------解决方案--------------------------------------------------------
你的Runnable后面为什么要加一个括号???你看可以解决你的问题吗?
----------------解决方案--------------------------------------------------------
程序代码:
public class Testsyn implements Runnable{//为什么实现一个接口他还报错呀
Timer timer = new Timer();//这个地方它显示的是分号错了
public static void main(String[] args) {
// TODO Auto-generated method stub
Testsyn t = new Testsyn();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
public void run() {
// TODO Auto-generated method stub
timer.add(Thread.currentThread().getName());
}
}
Timer timer = new Timer();//这个地方它显示的是分号错了
public static void main(String[] args) {
// TODO Auto-generated method stub
Testsyn t = new Testsyn();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
public void run() {
// TODO Auto-generated method stub
timer.add(Thread.currentThread().getName());
}
}
这样编译就没有问题了
至于实现功能,你在看看
----------------解决方案--------------------------------------------------------
----------------解决方案--------------------------------------------------------