java线程

package single_test;

import java.awt.print.Printable;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.Vector;

@SuppressWarnings({"rawtypes","unused","unchecked"})
public class ThreadTest implements Runnable{

private List elementList;
private List errorList = new Vector();


public ThreadTest(List elementList) {
this.elementList = elementList;
}

public List getErrorList() {
errorList.addAll(elementList);
elementList.clear();
return errorList;
}
public void addErrorList(String errorFn) {
this.errorList.add(errorFn);
}


public void run() {
while (!elementList.isEmpty()) {
String fn;
synchronized (elementList) {
fn = (String) elementList.remove(0);
}
System.out.println(fn);
try {
//上传
} catch (Exception e) {
//
this.errorList.add(fn);
}
}
}

public static void main(String[] args) {
List elementList = new Vector();
for(int i=0;i<1000;i++){
elementList.add(""+i);
}
ThreadTest tt = new ThreadTest(elementList);

Thread t1 = new Thread(tt);
Thread t2 = new Thread(tt);
Thread t3 = new Thread(tt);
Thread t4 = new Thread(tt);



t1.start();
t2.start();
t3.start();
t4.start();

try {
t1.join();
t2.join();
t3.join();
t4.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println("asdfasdflaskdfjlaksfj");

List fnList = tt.getErrorList();
System.out.println(fnList);
}
}

你可能感兴趣的:(java,thread)