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.