萌战吧 关注:964,114贴子:31,631,302
  • 26回复贴,共1

【战万作业求助】多线程与data race与program hanging

只看楼主收藏回复

这个问题以前也遇到过,永远不知道原因。。
这次是java的作业,multithreading,一段没有被syncronized的代码如果被运行,程序就会hang住,然后我想print点东西出来debug试试(我承认我菜),结果一加上print的code程序就不hang了,这里面是什么原因?


IP属地:上海1楼2014-05-11 17:30回复
    我知道吧里cs大佬多
    比如说要执行的是下面这个mode:
    class UnsynchronizedState implements State {
    private byte[] value;
    private byte maxval;
    UnsynchronizedState(byte[] v) { value = v; maxval = 127; }
    UnsynchronizedState(byte[] v, byte m) { value = v; maxval = m; }
    public int size() { return value.length; }
    public byte[] current() { return value; }
    public boolean swap(int i, int j) {
    if (value[i] <= 0 || value[j] >= maxval) {
    return false;
    }
    value[i]--;
    value[j]++;
    return true;
    }
    }
    然后有这一段
    class SwapTest implements Runnable {
    private int nTransitions;
    private State state;
    SwapTest(int n, State s) {
    nTransitions = n;
    state = s;
    }
    public void run() {
    int n = state.size();
    if (n != 0)
    for (int i = 0; i < nTransitions; ) {
    int a = ThreadLocalRandom.current().nextInt(0, n);
    int b = ThreadLocalRandom.current().nextInt(0, n - 1);
    if (a == b)
    b = n - 1;
    if (state.swap(a, b)) {
    i++;
    }
    }
    }
    最后有这么一段
    private static void dowork(int nThreads, int nTransitions, State s)
    throws InterruptedException {
    Thread[] t = new Thread[nThreads];
    for (int i = 0; i < nThreads; i++) {
    int threadTransitions =
    (nTransitions / nThreads
    + (i < nTransitions % nThreads ? 1 : 0));
    t[i] = new Thread (new SwapTest (threadTransitions, s));
    }
    long start = System.nanoTime();
    for (int i = 0; i < nThreads; i++)
    t[i].start ();
    for (int i = 0; i < nThreads; i++) {
    System.out.println("x"+i);
    t[i].join ();
    }
    long end = System.nanoTime();
    double elapsed_ns = end - start;
    System.out.format("Threads average %g ns/transition\n",
    elapsed_ns * nThreads / nTransitions);
    }
    机制是这一段
    you will use a simple prototype that manages a data structure that represents anarray of integers. Each integer is in the range [0,maxval] where maxval is at most 127, so the integer can be represented by the Java type byte. A statetransition, called a swap, consists of subtracting 1 from one of the positive integers in the array, and adding 1 to an integer that is less than maxval. Thesum of all the integers should therefore remain constant; if it varies, that indicates that one or more transitions weren't done correctly. Also, the values inthe array should always be in range.
    谁有时间的话就帮我看看吧


    IP属地:上海2楼2014-05-11 17:35
    回复
      运行java UnsafeMemory Unsynchronized 8 1000000 6 5 6 3 0 3万年假死 (8是线程数,1000000是transition的次数,6,5,6,3,0,3是state array里的element)
      把transition的次数减少一点有几率能成功(运行完)。。


      IP属地:上海3楼2014-05-11 17:39
      收起回复
        为什么会hang


        IP属地:北京4楼2014-05-11 18:07
        收起回复
          我以为你已经放假了。。
          原来是在苦逼的考试周过程中嘛?


          5楼2014-05-12 13:08
          收起回复
            新类别,战我不码。。?(好奇怪)


            IP属地:美国来自Android客户端6楼2014-05-12 15:43
            回复
              居然真的发了,帮你顶上去


              7楼2014-05-12 18:16
              收起回复
                一 一
                不会面向对象的沙包路过


                9楼2014-05-13 15:24
                回复