62,615
社区成员
发帖
与我相关
我的任务
分享
import java.util.Vector;
//实现Runnable接口的线程
public class HelloThread implements Runnable {
String name;
Vector v;
HelloThread(String name,Vector v) {
this.name = name;
this.v = v;
}
public void run() {
/*
* int sleeptime=(int)(Math.random()*3000); ///A try{ //B
* Thread.sleep(sleeptime); ///C }catch(InterruptedException e){} ///D
*/
System.out.println(name+"start");
v.add(name+".add");
}
public static void main(String args[]) throws InterruptedException {
Vector v = new Vector();
HelloThread hello1 = new HelloThread("hello1",v);
HelloThread hello2 = new HelloThread("hello2",v);
HelloThread hello3 = new HelloThread("hello3",v);
Thread h1 = new Thread(hello1);
Thread h2 = new Thread(hello2);
Thread h3 = new Thread(hello3);
h1.start();
h2.start();
h3.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
for(int i=0;i<v.size();i++){
System.out.println(v.get(i));
}
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
//实现Runnable接口的线程
public class HelloThread implements Runnable {
String name;
List v;
HelloThread(String name,List v) {
this.name = name;
this.v = v;
}
public void run() {
System.out.println(name+"start");
v.add(name+".add");
}
public static void main(String args[]) throws InterruptedException {
List v = new ArrayList();
HelloThread hello1 = new HelloThread("hello1",v);
HelloThread hello2 = new HelloThread("hello2",v);
HelloThread hello3 = new HelloThread("hello3",v);
Thread h1 = new Thread(hello1);
Thread h2 = new Thread(hello2);
Thread h3 = new Thread(hello3);
h1.start();
h2.start();
h3.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
System.out.println("v.size:"+v.size());
for(int i=0;i<v.size();i++){
System.out.println(v.get(i));
}
}
}
final List l = new ArrayList(1);
Thread t = new Thread() {
public void run() {
l.add("X");
}
};
t.start();
Thread t2 = new Thread() {
public void run() {
l.add("X");
}
};
t2.start();
t.join();
t2.join();
System.out.println(l.size());
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadSafeTest implements Callable<Boolean> {
private List<String> list;
public ThreadSafeTest(List<String> list) {
this.list = list;
}
@Override
public Boolean call() {
list.add(Thread.currentThread().getName());
return true;
}
public static void main(String[] args) {
List<String> list = new ArrayList<String>(); // not safe
// List<String> list = new Vector<String>(); // safe
ExecutorService e = Executors.newFixedThreadPool(5);
Set<ThreadSafeTest> tasks = new HashSet<ThreadSafeTest>();
long timeStart = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
tasks.add(new ThreadSafeTest(list));
}
try {
e.invokeAll(tasks);
} catch (InterruptedException e2) {
e2.printStackTrace();
}
System.out.println("cost time:"
+ (System.currentTimeMillis() - timeStart) +"ms");
System.out.println("list size:" + list.size());
e.shutdown();
}
}