- import java.util.*;
- public class Test5 {
-
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Add add=new Add();
- Thread t1=new Thread(add);
- Thread t2=new Thread(add);
- t1.start();
- t2.start();
-
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println(add.list);
- }
- }
- class Add implements Runnable{
- int i=0;
- List<Integer> list=new ArrayList<Integer>();
- Random r=new Random();
- public void run() {
- for (int x = 0; x < 3; x++) {
- synchronized (this) {
- this.notify();
- int y=r.nextInt(10);
- System.out.println(Thread.currentThread().getName()+"......"+y);
- list.add(y);
- try {
- this.wait();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
-
- }
复制代码
|
|