Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9119831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:29:21+00:00 2026-06-17T05:29:21+00:00

I have a program which task would be to proof that my class is

  • 0

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.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:29:23+00:00Added an answer on June 17, 2026 at 5:29 am

    You’re not running these threads simultaneously.

        threads[i].run();
    

    should read

        threads[i].start();
    

    Thread.start() actually spawns that thread. Thread.run() will just call the Runnable‘s run() method. From the doc:

    If this thread was constructed using a separate Runnable run object,
    then that Runnable object’s run method is called; otherwise, this
    method does nothing and returns.

    Not quite sure why that method exists. It’s a common source of confusion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program which I would like to run every X min the
I have a program which takes a long time to complete. I would like
I'm making a task-based program that needs to have plugins. Tasks need to have
I have thread application which process heavy task, i would like to trigger forcibly
So I got this task to make a program which will allow the user
Task: have a jailbroken iPhone need to write an application which would receive notifications
I have the task of creating two seperate programs, one linear search program, which
I have a program which performs a useful task. Now I want to produce
I have a program which creates JButtons which are then added to a JPanel
I have a program which dynamically generates a GUI. I don't know how many

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.