public class Test implements Runnable { public List<Integer> l; public Boolean notFinish = true; public Test(){ l = new ArrayList<Integer>(); }
@Override public void run() { while (true){ try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized(this){ if (l.size()>0){ int sum = 0; for (Integer i:l){ sum = sum + i; } System.out.println(sum / l.size()); } if (!notFinish) break; } } }
public static void main(String[] args){ Test test = new Test(); Thread output = new Thread(test); output.start(); Scanner s=new Scanner(System.in); int i; while ( ( i = Integer.parseInt(s.next())) != 0){ synchronized(test){ test.l.add(i); } } synchronized(test){ test.notFinish = false; } } }
public class Test {
public static void main(String[] args) {
List<Integer> scoreList=new ArrayList<Integer>(); new Thread (new T1(scoreList)).start(); int j=80; while(scoreList.size()==0||scoreList.get(scoreList.size()-1)!=0){ scoreList.add(j); j=j-5; try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
}} public class T1 implements Runnable {
private List<Integer> sorceList;
public T1(List<Integer> l){ this.sorceList=l; } @Override public void run() {
public int getAvgScore(){ int total=0; if(this.sorceList.size()!=0){ for(Integer score:this.sorceList){ total+=score; } return (total/this.sorceList.size()); } return 0;
线程1 获取控制台输入的数字,如输入的数字不为0 则保存到集合中 若为0 则线程结束
用Scanner scan=new Scanner(System.in);
线程2 就是把集合中的数字平均下并输出 此方法10s执行一次 (类似定时器)
思路就是这样的,具体代码LZ 自己写吧
main建个集合 线程1不知道怎么对它操作了
求解
public List<Integer> l;
public Boolean notFinish = true;
public Test(){
l = new ArrayList<Integer>();
}
@Override
public void run() {
while (true){
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(this){
if (l.size()>0){
int sum = 0;
for (Integer i:l){
sum = sum + i;
}
System.out.println(sum / l.size());
}
if (!notFinish) break;
}
}
}
public static void main(String[] args){
Test test = new Test();
Thread output = new Thread(test);
output.start();
Scanner s=new Scanner(System.in);
int i;
while ( ( i = Integer.parseInt(s.next())) != 0){
synchronized(test){
test.l.add(i);
}
}
synchronized(test){
test.notFinish = false;
}
}
}
public static void main(String[] args) {
List<Integer> scoreList=new ArrayList<Integer>();
new Thread (new T1(scoreList)).start();
int j=80;
while(scoreList.size()==0||scoreList.get(scoreList.size()-1)!=0){
scoreList.add(j);
j=j-5;
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}}
public class T1 implements Runnable {
private List<Integer> sorceList;
public T1(List<Integer> l){
this.sorceList=l;
} @Override
public void run() {
while(true){
System.out.println("平均分:"+getAvgScore());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public int getAvgScore(){
int total=0;
if(this.sorceList.size()!=0){
for(Integer score:this.sorceList){
total+=score;
}
return (total/this.sorceList.size());
}
return 0;
}大概代码是这样的 自己再稍微修改下!