001 /* 002 * @(#)ConcurrentList.java 0.1 03/05/19 003 * 004 * Copyright (c) 2003 Arno Formella and Jose B. Gonzalez Lopez. 005 * All Rights Reserved. 006 * 007 * LGPL 008 * 009 */ 010 011 /* a possible package location */ 012 // package es.uvigo.ei.cd.java.util.concurrent; 013 014 import java.util.*; 015 016 /** 017 Program to test the implementation of the ConcurrentList data structure. 018 019 @author Arno Formella and José B. González López 020 @version 0.1, 05/19/03 021 */ 022 public class TestThread implements Runnable { 023 static final int PR_SIZE=100; 024 public static ConcurrentList concurrentList; 025 private ConcurrentItem privateList[]; 026 String name = ""; 027 028 TestThread(String n) { 029 name = n; 030 privateList = new ConcurrentItem[PR_SIZE]; 031 for(int i = 0; i<PR_SIZE; i++) 032 privateList[i] = null; 033 } 034 035 public void run() { 036 for(int i = 0; i<PR_SIZE; i++) 037 privateList[i] = null; 038 try { 039 for(int i = 1; i<PR_SIZE; i++) { 040 Double d = new Double(Math.random() * PR_SIZE); 041 int pos = d.intValue(); 042 if(privateList[pos] == null) { 043 d = new Double(Math.random() * PR_SIZE); 044 int pos2 = d.intValue(); 045 privateList[pos] = privateList[pos2] == null? 046 concurrentList.addFirst(new Integer(i)): 047 concurrentList.addItem(new Integer(i),privateList[pos2]); 048 Thread.sleep(pos2); 049 } 050 else { 051 concurrentList.removeItem(privateList[pos]); 052 privateList[pos] = null; 053 } 054 Thread.sleep(pos); 055 } 056 } 057 catch(Exception e) { 058 System.out.println("\n erroooorrr["+name+"]: "+e); 059 e.printStackTrace(); 060 } 061 System.out.println("\nterminated ["+name+"]"); 062 } 063 064 public static void main(String args[]) { 065 try { 066 concurrentList = new ConcurrentList(); 067 068 TestThread h1 = new TestThread("thread1"); 069 TestThread h2 = new TestThread("thread2"); 070 Thread t1 = new Thread((Runnable)h1); 071 Thread t2 = new Thread((Runnable)h2); 072 t1.start(); 073 t2.start(); 074 075 /* 076 uses conversion to array 077 */ 078 while(t1.isAlive() && t2.isAlive()) { 079 Object[] a=concurrentList.toArray(); 080 for(int index=0;index<a.length;index++) { 081 Integer integer=(Integer)a[index]; 082 System.out.print(integer.toString()); 083 } 084 System.out.println(); 085 Thread.sleep(50); 086 } 087 t1.join(); 088 t2.join(); 089 090 System.out.println("clearing list"); 091 concurrentList.clear(); 092 System.out.println("list cleared"); 093 094 h1 = new TestThread("thread3"); 095 h2 = new TestThread("thread4"); 096 t1 = new Thread((Runnable)h1); 097 t2 = new Thread((Runnable)h2); 098 t1.start(); 099 t2.start(); 100 101 /* 102 uses iterator over elements 103 */ 104 while(t1.isAlive() && t2.isAlive()) { 105 ConcurrentIterator i = concurrentList.listIterator(); 106 try{ 107 Integer element; 108 ConcurrentItem paramItem= new ConcurrentItem();; 109 while((element = (Integer)i.next()) != null) { 110 System.out.print(element.toString()); 111 } 112 } 113 catch(NoSuchElementException e) { 114 } 115 System.out.println(); 116 Thread.sleep(50); 117 } 118 t1.join(); 119 t2.join(); 120 121 h1 = new TestThread("thread5"); 122 h2 = new TestThread("thread6"); 123 t1 = new Thread((Runnable)h1); 124 t2 = new Thread((Runnable)h2); 125 t1.start(); 126 t2.start(); 127 128 /* 129 uses iterator over items 130 */ 131 while(t1.isAlive() && t2.isAlive()) { 132 ConcurrentIterator i = concurrentList.listIterator(); 133 ConcurrentItem paramItem= new ConcurrentItem();; 134 while(i.nextItem(paramItem) != null) { 135 Integer integer=(Integer)paramItem.getElement(); 136 System.out.print(integer.toString()); 137 } 138 System.out.println(); 139 Thread.sleep(50); 140 } 141 t1.join(); 142 t2.join(); 143 144 } 145 catch(Throwable e) { 146 e.printStackTrace(); 147 System.out.println("e: " +e); 148 } 149 } 150 }