1

I have a program which task would be to proof that my class is not threadsafe, but i got good results, but can' understand why....

The application is basicly increase all workers salary by one, and do it 1000times and with 100 threads so the results should be increased with 100.000, and it is OK :/

Testing class:

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);
        }

Thread class:

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());
            }
        }
    }

*RESULTS:*

-------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()を呼び出すだけです。ドキュメントから:Runnablerun()

このスレッドが別のRunnablerunオブジェクトを使用して構築された場合、そのRunnableオブジェクトのrunメソッドが呼び出されます。それ以外の場合、このメソッドは何もせずに戻ります。

その方法が存在する理由はよくわかりません。これは一般的な混乱の原因です。

于 2013-01-11T17:03:12.387 に答える