线程似乎工作正常,但不应该

时间:2013-01-11 17:01:32

标签: java multithreading concurrency

我有一个程序,其任务是证明我的班级不是线程安全的,但我得到了很好的结果,但可以'理解为什么......

应用程序基本上将所有工人的工资增加一倍,并且执行1000次并使用100个线程,因此结果应该增加100.000,并且没关系:/

测试类:

final int THREADGROUPSIZE = 100;
        Thread threads[] = new Thread[THREADGROUPSIZE];

        for (int i=0; i <THREADGROUPSIZE; ++i) {
            threads[i] = new Thread(new CompManagerThread());
            threads[i].run();
        }

        for (int i=0; i <THREADGROUPSIZE; ++i) {
            try {
                threads[i].join();
            }
            catch (InterruptedException e) {
                System.out.print("Join interrupted\n");
            }
        }

        System.out.print("Waiting for threads to complete\n");
        System.out.println("-------PRINTING THREADED WORKERS----------");
        Company threadCompany = Company.getInstance();
        ArrayList<Worker> workerThreadArray = threadCompany.GetWorkersOrderedInSalary();
        for (Worker worker : workerThreadArray) {
            System.out.println(worker);
        }

        System.out.println("-------PRINTING ORIGIN WORKERS----------");
        ArrayList<Worker> workerArray = companyFromHSQL.GetWorkersOrderedInSalary();
        for (Worker worker : workerArray) {
            System.out.println(worker);
        }

主题类:

public class CompManagerThread implements  Runnable {

        Company threadCompany;

        CompManagerThread(){
            threadCompany = Company.getInstance();
        }

        @Override
        public void run() {
            for (int i = 0; i < 1000; i++) {
                increaseSalary(1);
                increaseBudget(1);
            }

    //        for (int i = 0; i < 1000; i++) {
    //           decreaseSalary(1);
    //           decreaseBudget(1);
    //        }
        }

        /**
         * Increase the Salary by one.
         *
         * @param number we increasing the salaries
         */
        public void increaseSalary(Number number) {
            for (Map.Entry<String, Worker> workerEntry : threadCompany.getAllWorkers().entrySet()) {
                workerEntry.getValue().setSalary(workerEntry.getValue().getSalary().intValue() + number.intValue());
            }
        }

        /**
         * Increase the Budget by one.
         *
         * @param number we increasing the budgets
         */
        public void increaseBudget(Number number) {
            for (Map.Entry<String, Project> projectEntry : threadCompany.getProjects().entrySet()) {
                projectEntry.getValue().setBudget(projectEntry.getValue().getBudget().intValue() + number.intValue());
            }
        }

        /**
         * Decrease the Salary by one.
         *
         * @param number we decreasing the salaries
         */
        public void decreaseSalary(Number number) {
            for (Map.Entry<String, Worker> workerEntry : threadCompany.getAllWorkers().entrySet()) {
                workerEntry.getValue().setSalary(workerEntry.getValue().getSalary().intValue() - number.intValue());
            }
        }


        /**
         * Decrease the Budget by one.
         *
         * @param number we decreasing the budgets
         */
        public void decreaseBudget(Number number) {
            for (Map.Entry<String, Project> projectEntry : threadCompany.getProjects().entrySet()) {
                projectEntry.getValue().setBudget(projectEntry.getValue().getBudget().intValue() - number.intValue());
            }
        }
    }

*的 结果: *

-------PRINTING THREADED WORKERS----------
Fejlesztő János is a WorkerDeveloper and has 310000HuF salary.
Fejlesztő Csaba is a WorkerDeveloper and has 350000HuF salary.
Fejlesztő Béla is a WorkerDeveloper and has 400000HuF salary.
Konzulens Béla is a WorkerConsultant and has 400000HuF salary.
Konzulens Csaba is a WorkerConsultant and has 410000HuF salary.
ProjectVezető Béla is a LeaderProject and has 500000HuF salary.
VezetőFejlesztő Béla is a LeaderDeveloper and has 500000HuF salary.
VezetőFejlesztő Csaba is a LeaderDeveloper and has 500000HuF salary.
ProjectVezető János is a LeaderProject and has 500000HuF salary.
ProjectVezető Csaba is a LeaderProject and has 500000HuF salary.
VezetőFejlesztő János is a LeaderDeveloper and has 500000HuF salary.
VezetőKonzulens Béla is a LeaderConsultant and has 600000HuF salary.
VezetőKonzulens János is a LeaderConsultant and has 610000HuF salary.
VezetőKonzulens Csaba is a LeaderConsultant and has 620000HuF salary.
-------PRINTING ORIGIN WORKERS----------
Fejlesztő János is a WorkerDeveloper and has 210000.0HuF salary.
Fejlesztő Csaba is a WorkerDeveloper and has 250000.0HuF salary.
Fejlesztő Béla is a WorkerDeveloper and has 300000.0HuF salary.
Konzulens Béla is a WorkerConsultant and has 300000.0HuF salary.
Konzulens Csaba is a WorkerConsultant and has 310000.0HuF salary.
ProjectVezető Béla is a LeaderProject and has 400000.0HuF salary.
VezetőFejlesztő Béla is a LeaderDeveloper and has 400000.0HuF salary.
VezetőFejlesztő Csaba is a LeaderDeveloper and has 400000.0HuF salary.
ProjectVezető János is a LeaderProject and has 400000.0HuF salary.
ProjectVezető Csaba is a LeaderProject and has 400000.0HuF salary.
VezetőFejlesztő János is a LeaderDeveloper and has 400000.0HuF salary.
VezetőKonzulens Béla is a LeaderConsultant and has 500000.0HuF salary.
VezetőKonzulens János is a LeaderConsultant and has 510000.0HuF salary.
VezetőKonzulens Csaba is a LeaderConsultant and has 520000.0HuF salary.

1 个答案:

答案 0 :(得分:8)

您没有同时运行这些线程。

    threads[i].run();

应该阅读

    threads[i].start();

Thread.start()实际上生成该线程。 Thread.run()只会调用Runnable的{​​{1}}方法。来自doc:

  

如果使用单独的Runnable运行对象构造此线程,   然后调用Runnable对象的run方法;否则,这个   方法什么都不做并且返回。

不太确定该方法存在的原因。这是混淆的常见原因。

相关问题