小弟在做thinkin java上面的题目 用的是eclipse
有一个类无法编译 类名我确实检查过了 是叫ex17 下面是我自己写的代码import java.util.*;
import java.util.concurrent.*;public class ex17 { /**
* @param args
*/
public static void main(String[] args) throws Exception {
ExecutorService exec = Executors.newCachedThreadPool();
for(int i = 0;i<10; i++){
exec.execute(new ex17_sensoro(i));
}
System.out.println("Total: " + ex17_sensoro.getTotalCount());
System.out.println("Sun of Sensoro: " + ex17_sensoro.sumSensoro());
}}
class ex17_count{
private int count = 0;
public synchronized int increment(){
int temp = count;
return (count = ++temp);
}
public synchronized int value(){ return count;}
}class ex17_sensoro implements Runnable{
private static ex17_count count = new ex17_count();
private static List<ex17_sensoro> sensoro = new ArrayList<ex17_sensoro>();
private int number = 0; // the number of one sensoro
private final int id; // id of sensoro
public ex17_sensoro(int id){
this.id = id;
sensoro.add(this);
}
public void run(){
synchronized(this){
++number;
System.out.println(this + "Total: " + count.increment());
}
}
public synchronized int getValue(){return number;}
public String toString(){
return "Sensoro "+ id + ":" + getValue();
}
public static int getTotalCount(){
return count.value();
}
public static int sumSensoro(){
int sum = 0;
for(ex17_sensoro sensoro1 : sensoro)
sum += sensoro1.getValue();
return sum;
}
}
/* output
java.lang.NoClassDefFoundError: ex17
Exception in thread "main"
急死我了在线等 谢谢
有一个类无法编译 类名我确实检查过了 是叫ex17 下面是我自己写的代码import java.util.*;
import java.util.concurrent.*;public class ex17 { /**
* @param args
*/
public static void main(String[] args) throws Exception {
ExecutorService exec = Executors.newCachedThreadPool();
for(int i = 0;i<10; i++){
exec.execute(new ex17_sensoro(i));
}
System.out.println("Total: " + ex17_sensoro.getTotalCount());
System.out.println("Sun of Sensoro: " + ex17_sensoro.sumSensoro());
}}
class ex17_count{
private int count = 0;
public synchronized int increment(){
int temp = count;
return (count = ++temp);
}
public synchronized int value(){ return count;}
}class ex17_sensoro implements Runnable{
private static ex17_count count = new ex17_count();
private static List<ex17_sensoro> sensoro = new ArrayList<ex17_sensoro>();
private int number = 0; // the number of one sensoro
private final int id; // id of sensoro
public ex17_sensoro(int id){
this.id = id;
sensoro.add(this);
}
public void run(){
synchronized(this){
++number;
System.out.println(this + "Total: " + count.increment());
}
}
public synchronized int getValue(){return number;}
public String toString(){
return "Sensoro "+ id + ":" + getValue();
}
public static int getTotalCount(){
return count.value();
}
public static int sumSensoro(){
int sum = 0;
for(ex17_sensoro sensoro1 : sensoro)
sum += sensoro1.getValue();
return sum;
}
}
/* output
java.lang.NoClassDefFoundError: ex17
Exception in thread "main"
急死我了在线等 谢谢
F:\javatest>java ex17
Sensoro 0:1Total: 1
Sensoro 1:1Total: 2
Sensoro 2:1Total: 3
Sensoro 3:1Total: 4
Sensoro 4:1Total: 5
Sensoro 5:1Total: 6
Sensoro 6:1Total: 7
Sensoro 7:1Total: 8
Sensoro 8:1Total: 9
Sensoro 9:1Total: 10
Total: 10
Sun of Sensoro: 10
这个就奇怪了 为什么我的就不可以执行 有人碰到过吗?我确实java文件名叫 ex17 的
Sensoro 1:1Total: 2
Sensoro 3:1Total: 3
Sensoro 4:1Total: 4
Sensoro 5:1Total: 5
Sensoro 6:1Total: 6
Sensoro 7:1Total: 7
Sensoro 8:1Total: 8
Total: 8
Sun of Sensoro: 8
Sensoro 2:1Total: 9
Sensoro 9:1Total: 10我的也可以运行
Sensoro 1:1Total: 2
Sensoro 3:1Total: 3
Sensoro 4:1Total: 4
Sensoro 5:1Total: 5
Sensoro 6:1Total: 6
Sensoro 7:1Total: 7
Sensoro 8:1Total: 8
Total: 8
Sun of Sensoro: 8
Sensoro 2:1Total: 9
Sensoro 9:1Total: 10我的也可以运行
eclipse啊 其他的java文件都可以执行的 就这个不可以
新建工程测试,或者clean工程,重启ec。直到没X为止,可以通过problem视图查看现在工程的错误情况。
然后把这个类再放进去,再执行一次试试看
这样应该就可以解决了