package org.test.test;import java.util.ArrayList; import java.util.List;public class UnSafeThread extends Thread { UnSafeObject unSafe; int dest; int value;
String id; public static void main(String[] args) { UnSafeObject unSafe = new UnSafeObject(); Thread a = new UnSafeThread(unSafe,0,3,"TA"); Thread b = new UnSafeThread(unSafe,0,33,"TB"); a.start(); b.start(); } public UnSafeThread() { } public UnSafeThread(UnSafeObject o, int dest, int value,String id) { unSafe=o; this.dest=dest; this.value=value; this.id=id; } public void run() { unSafe.l.add(dest,value); System.out.println(id+"的位置"+dest+" 现在设置为 "+value); try { Thread.sleep(1); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } unSafe.l.get(dest); System.out.println(id+"的位置"+dest+" = "+unSafe.l.get(dest)); } }class UnSafeObject { List l = new ArrayList();; static UnSafeObject obj = new UnSafeObject(); public UnSafeObject() { }}
unSafe.l.add(dest,value); == > unSafe.l.add(dest, new Integer(value));
import java.util.List;public class UnSafeThread extends Thread {
UnSafeObject unSafe; int dest; int value;
String id; public static void main(String[] args) {
UnSafeObject unSafe = new UnSafeObject();
Thread a = new UnSafeThread(unSafe,0,3,"TA");
Thread b = new UnSafeThread(unSafe,0,33,"TB");
a.start();
b.start();
} public UnSafeThread() {
} public UnSafeThread(UnSafeObject o, int dest, int value,String id) {
unSafe=o;
this.dest=dest;
this.value=value;
this.id=id;
} public void run() {
unSafe.l.add(dest,value);
System.out.println(id+"的位置"+dest+" 现在设置为 "+value);
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
unSafe.l.get(dest);
System.out.println(id+"的位置"+dest+" = "+unSafe.l.get(dest));
}
}class UnSafeObject {
List l = new ArrayList();; static UnSafeObject obj = new UnSafeObject(); public UnSafeObject() {
}}
=========================
你的程序用sleep,我想要的是不要sleep也会出现问题
还有你的程序没体现出问题来,用Vector和List输出一个样.我想要的效果是用Vector或者Collections.synchronizedXXX()不会出现线程安全问题,而单用List会出现问题
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Vector;public class UnSafeThread<E> extends Thread {
List v;
int id; public static void main(String[] args) { List l = new AbstractList() {
public String get(int i) {
return "";
} public void add(int index, Integer element) {
} public String remove(int index) {
if (index == 24) {
System.out.println("正在删除" + index + "个元素");
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("删除" + index + "个元素结束");
}
if (index == 23) {
System.out.println("正在删除" + index + "个元素");
System.out.println("删除" + index + "个元素结束");
}
return "";
} public int size() {
return 0;
} };
List l2 = Collections.synchronizedList(l);
Thread ta = new UnSafeThread(l, 1);
Thread tb = new UnSafeThread(l, 2);
ta.start();
tb.start();
} public UnSafeThread(List o, int id) {
v = o;
this.id = id;
} public void run() {
if (id == 1) {
v.remove(24); } else {
v.remove(23); } }
}
我举一个没用sleep但是会出现线程安全问题的例子(但是这个例子和Collection无关,我的目的就是想让写一个和Collection有关的)
class SychronizedTest
{
public static void main(String[] args)
{
final Student stu = new Student();
Thread setNameAndNumth1 = new Thread( new Runnable(){
public void run(){
while(true){
synchronized(stu){
stu.setNameAndNum("john","jj");
}
}
}
});
Thread setNameAndNumth2 = new Thread( new Runnable(){
public void run(){
while(true){
//synchronized(stu){
stu.setNameAndNum("kate","kk");
//}
}
}
});
setNameAndNumth1.start();
setNameAndNumth2.start();
System.out.println("test started:");
}
}
class Student{
private String name;
private String id;
private int count = 0;
public /*synchronized*/ void setNameAndNum(String name,String id){
// synchronized(this){
this.name = name;
this.id = id;
count++;
if(!check())
System.out.println( count + " unmatched name and id: "+name+" "+id);
// }
}
private boolean check(){
return name.charAt(0) == id.charAt(0);
}
}
----------------------------
正在删除24个元素<------------ta
正在删除23个元素
删除23个元素结束
删除24个元素结束<------------这时删除的是原来的第25个元素
使用匿名同步list的时候
正在删除24个元素
删除24个元素结束
正在删除23个元素
删除23个元素结束不知道是不是你没看懂我的例子
======================
估计你没有运行过我的例子
======================
正是想要用程序来证明用与不用synchronizedList的区别