0

我的作业是使用并行化创建项目,一切都应该是正确的。但是,我做了我的项目,但我的教授在我的代码中提到了一些错误“请查看数组列表,有些东西不对,也许是同步?”。

我想请你们社区帮助我并指出可能出了什么问题。我认为没有用synchronize括号覆盖我的数组列表可能是个问题,对吗?

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

    /**
    * My project finds all dividors for specific number
    *It must use threads, so I made them. First I start them (first loop) 
    *then join them (second loop). My project must have that loops.
    *Problem might be with not synchronizing methods array list...
    */

    public class Main {

        private final static int NUMBER = 100;
        private final static List<Integer> dividors = new ArrayList<Integer>();

        public static void main(String[] args) {
            new Main().doStuff();
        }
        private int sqr;
        private int sqrp1;

        private void doStuff() {

            sqr = (int) Math.sqrt(NUMBER);
            sqrp1 = sqr + 1;

            Thread[] t = new Thread[sqrp1];

        //starting tasks
            for (int i = 1; i < sqrp1; i++) {
                final int it = i;

                if (NUMBER % i == 0) {
                    final int e = i;

                    t[i] = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            System.out.println("sta"+e);
                            if (!checkContains(e)) {
                                addElement(e);
                            }

                            final int dividednumber = NUMBER / e;

                            if (!checkContains(dividednumber)) {
                                addElement(dividednumber);
                            }
                        }
                    });
                    t[i].start();
                }
            }

        //calling join for tasks
            for (int i = 1; i < sqrp1; i++) {
                final int it = i;

                if (NUMBER % i == 0) {
                    try {
                        System.out.println("sto"+i);
                        t[i].join();
                    } catch (InterruptedException ex) {
                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }


            System.out.println("xxx");

            Collections.sort(dividors);
            Integer[] arrayDividors = dividors.toArray(new Integer[0]);

            for (int i = 0; i < arrayDividors.length; i++) {
                System.out.println(arrayDividors[i]);
            }
        }

        private synchronized void addElement(int element) {
            dividors.add(element);
        }

        private synchronized boolean checkContains(int element) {
            return dividors.contains(element);
        }
    }

我更改这部分是否正确,现在可以吗?

t[i] = new Thread(new Runnable() {
    @Override
    public void run() {
        System.out.println("waiting " + e);
        synchronized (this) {
            System.out.println("entering " + e);

            if (!checkContains(e)) {
                addElement(e);
            }

            final int dividednumber = NUMBER / e;

            if (!checkContains(dividednumber)) {
                addElement(dividednumber);
            }
            System.out.println("leaving " + e);
        }
    }
});
4

1 回答 1

1

您需要将其转换为单个原子操作。

 if (!checkContains(dividednumber)) {
     addElement(dividednumber);
 }

想象一下你有两个线程。

 T1:  if (!checkContains(dividednumber)) { // false
 T2:  if (!checkContains(dividednumber)) { // false
 T1:      addElement(dividednumber); // adds number
 T2:      addElement(dividednumber); // adds same number

如果您有一个 addElementWithoutDuplicates,则不会发生这种情况。

于 2013-05-31T07:34:49.500 回答