huayurei77
huayurei77
2011-08-01 01:13

java.util.Stack是线程安全的么?

已采纳

最近在学习java的并发包编程,写了一段代码,模仿原来测试ArrayList非安全代码来测试java.util.Stack是否是线程安全的。代码如下:
[code="java"]
package concurrent.lock.alg;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class StackTest {
private static final int ELEMENT_NUM = 51180;
public static void main(String[] ben){
ExecutorService exec = Executors.newCachedThreadPool();
final Stack stackStr = new Stack();
for(int i=0;i<ELEMENT_NUM;++i){
exec.submit(new StackInsTask(stackStr));
}
exec.shutdown();
try{
exec.awaitTermination(500, TimeUnit.SECONDS);
}catch(InterruptedException e){
e.printStackTrace();
}

    List<String> testList = new ArrayList<String>();
    while(!stackStr.empty()){
        String dd = String.valueOf(stackStr.pop());
        //System.out.println(dd);
        testList.add(String.valueOf(dd));
    }
    //System.out.println(testList.size());
    for(int i=1;i<=ELEMENT_NUM;++i){
        if(!testList.contains(String.valueOf(i))){
            System.out.println("didn't find"+i);
        }
    }
}

}
class StackInsTask implements Runnable {
public static AtomicInteger count = new AtomicInteger();
Stack stack;

public StackInsTask(Stack s) {
    stack = s;
}

public void run() {
    stack.push(count.incrementAndGet());
}

}

[/code]
51180个线程同时启动,如果stack非安全,那么stack.push(count.incrementAndGet())插入的可能是同一个值。那么在:
[code="java"]
for(int i=1;i<=ELEMENT_NUM;++i){
if(!testList.contains(String.valueOf(i))){
System.out.println("didn't find"+i);
}
}
[/code]
[b]就会输出找不到的值,可是测试了几把发现好像是安全的,由于jdk也没说明stack是否是线程安全的,暂时没看源码,所以好奇来问下。顺便问下,eclipse看jdk源码需要下载些什么,这里偷个懒嘿嘿~[/b]

---------------------------------分割线,以下代码是证明ArrayList非安全的代码--------------------------------------
[code="java"]
package com.concurrent.Amino.List;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class CommonListTest {
private static final int ELEMENT_NUM = 28000;

public static void main(String[] argvs) {
    ExecutorService exec = Executors.newCachedThreadPool();

// final List listStr = new LockFreeList();
final List listStr = new ArrayList();
for (int i = 0; i < ELEMENT_NUM; ++i) {
exec.submit(new ListInsTask(listStr));
}

    exec.shutdown();

    try {
        exec.awaitTermination(500, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    System.out.println("Size of list is" + listStr.size());

    for (int i = 1; i <= ELEMENT_NUM; ++i) {
        if (!listStr.contains(i)) {
            System.out.println("didn't find " + i);
        }
    }
    System.out.println(ListInsTask.count.longValue());
}

}

class ListInsTask implements Runnable {
public static AtomicInteger count = new AtomicInteger();
List list;

public ListInsTask(List l) {
    list = l;
}

public void run() {
    if (list.add(count.incrementAndGet())) {
        //System.out.println("List Size = " + list.size());
    } else {
        System.out.println("did not insert" + count.get());
    }

}

}
[/code]

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • weixin_42560972 也许世界还没停 10年前

    Stack 继承自 Vector,是线程安全的
    似乎不太推荐这种老的容器了

    看源码这东西网上一搜一堆,就不转了

    点赞 评论 复制链接分享

相关推荐