1

我有一个程序,其任务是证明我的类不是线程安全的,但我得到了很好的结果,但不明白为什么......

该应用程序基本上是将所有工人的工资增加一倍,并使用 100 个线程执行 1000 次,因此结果应该增加 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.
4

1 回答 1

8

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

    threads[i].run();

应该读

    threads[i].start();

Thread.start()实际上产生了那个线程。Thread.run()只会调用Runnable'run()方法。从文档:

如果该线程是使用单独的 Runnable 运行对象构造的,则调用该 Runnable 对象的 run 方法;否则,此方法不执行任何操作并返回。

不太清楚为什么存在这种方法。这是一个常见的混淆来源。

于 2013-01-11T17:03:12.387 回答