package com.heima.lianxi;
import java.util.ArrayList;
import java.util.Random;
public class demo {
static ArrayList<Integer> al = new ArrayList<Integer>();
static Object[] obj;
public static void main(String[] args) throws Exception {
RR r = new RR();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
int n=1;
for (int i = 0; i < 2; i++) {
if(n==1){
t1.start();
t1.join();
}else{
t2.start();
t2.join();
}
n=(n+1)%2;
}
obj = al.toArray();
for (int i = 0; i < obj.length; i++) {
System.out.println(obj[i]);
}
}
static class RR implements Runnable {
public synchronized void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 3; i++) {
al.add(new Random().nextInt(10));
}
}
}
}
我就知道这一种不知道 嗨有没更简单的,可以相互讨论下 |