Good day. In the process of writing a program in Java using threads, I ran into a problem. The program started 8 threads at once. Each thread should display messages like:
1 Andrew Removed from the account 98 Now for the account: 4902
where the first digit is:
private int id = 1;
and access to it is realized through
public synchronized int idPlus() { return id++; } public synchronized int getId() { return id; }
part of the code of the executed thread:
bank.putOn(currsumm); String s = getId() + " " + Thread.currentThread().getName() + " Положил на счёт " + currsumm + " Cейчас на счёту: " + bank.getBalance(); System.out.println(s); idPlus();
But when I run the program, the variable id with the same value can pop up several times. And it turns out that something like this:
1 Boris Withdrawn from 106 Now on the account: 4751
1 Galya Withdrawn from account 63 Now on the account: 4751
1 Valentin Withdrawn from the account 80 Now for the account: 4751
1 Andrew Put at the expense of 99 Now on the account: 4850
Already declared the id volatile variable and called join () on the threads; What can you suggest with on this issue?
All code: https://ideone.com/MygrDN