Hello guys!!!
I have got a problem in program codes (from the book, Head First
Java 2nd ed., 'Java 5.0'). The output isn't given in the book. I have Java 7 installed (jdk1.7.0_05).
The programs are compile and run, but the second or third program is not giving the output it appears it should give.. Can you spot the problem in the codes I am giving below? And how to fix it? I could not find the 'synchronized' keyword in the Java 5.0 API. Can you tell where it can be located?
Problem: I don't know why the problem is occuring, and the three programs are giving the similar output even when the method 'increment()' is synchronized. Of the codes below, the first two programs are same as given in the book, the third is that I have modified using the second one.
There are three programs here: 1. TestSyncTest.java
2. Tst2.java
3. Tst3.java
The post is long due to the programs' outputs, but the output is simple.
Program 1: TestSyncTest.java
It gives the output as: (see the 'bold' and the 'dark red' colored text)
-----------------------
balance is 1
balance is 3
balance is 2
balance is 4
balance is 5
balance is 6
balance is 7
balance is 8
balance is 9
balance is 10
balance is 11
balance is 13
balance is 14
balance is 15
balance is 16
balance is 17
balance is 18
balance is 19
balance is 20
balance is 21
balance is 22
balance is 23
balance is 24
balance is 25
balance is 26
balance is 27
balance is 28
balance is 29
balance is 30
balance is 31
balance is 32
balance is 33
balance is 34
balance is 35
balance is 36
balance is 37
balance is 38
balance is 39
balance is 40
balance is 41
balance is 42
balance is 43
balance is 44
balance is 45
balance is 46
balance is 47
balance is 12
balance is 49
balance is 50
balance is 51
balance is 52
balance is 53
balance is 54
balance is 55
balance is 56
balance is 57
balance is 58
balance is 59
balance is 60
balance is 61
balance is 62
balance is 63
balance is 64
balance is 65
balance is 66
balance is 67
balance is 68
balance is 69
balance is 70
balance is 71
balance is 72
balance is 73
balance is 74
balance is 75
balance is 76
balance is 77
balance is 78
balance is 79
balance is 80
balance is 81
balance is 82
balance is 83
balance is 84
balance is 85
balance is 86
balance is 87
balance is 88
balance is 89
balance is 90
balance is 91
balance is 48
balance is 92
balance is 93
balance is 94
balance is 95
balance is 96
balance is 97
balance is 98
balance is 99
balance is 100
------------------------
Bold text indicates- the last 'balance' update is lost.
Dark red color indicates- a +2 increment, that should not happen, because we are doing +1 to the balance.
It should not happen that after balance 3, the balance is 2, but as here the increment method is not 'synchronized', it may happen. Now, see the second and third programs (and their output) where the method 'increment()' is synchronized.
Program 2: Tst2.java
Program 2's output is:
---------------------------
balance is 1
balance is 2
balance is 3
balance is 4
balance is 5
balance is 6
balance is 7
balance is 8
balance is 9
balance is 10
balance is 11
balance is 12
balance is 13
balance is 14
balance is 15
balance is 17
balance is 18
balance is 16
balance is 19
balance is 20
balance is 21
balance is 22
balance is 23
balance is 24
balance is 25
balance is 26
balance is 27
balance is 28
balance is 29
balance is 30
balance is 32
balance is 33
balance is 31
balance is 34
balance is 35
balance is 36
balance is 37
balance is 38
balance is 39
balance is 40
balance is 41
balance is 43
balance is 42
balance is 44
balance is 45
balance is 46
balance is 47
balance is 48
balance is 49
balance is 50
balance is 51
balance is 52
balance is 53
balance is 54
balance is 55
balance is 56
balance is 57
balance is 58
balance is 59
balance is 60
balance is 61
balance is 62
balance is 64
balance is 65
balance is 66
balance is 67
balance is 63
balance is 68
balance is 69
balance is 70
balance is 71
balance is 72
balance is 73
balance is 74
balance is 75
balance is 76
balance is 77
balance is 78
balance is 79
balance is 80
balance is 81
balance is 82
balance is 83
balance is 84
balance is 85
balance is 86
balance is 87
balance is 88
balance is 89
balance is 91
balance is 92
balance is 93
balance is 94
balance is 95
balance is 96
balance is 97
balance is 98
balance is 90
balance is 99
balance is 100
---------------------------
Silmilar probelem here too..
I did then modify this second program thinking that the 'balance' variable here must be acting here as 'local' to each of the two threads. The modified program is below.
Program 3: Tst3.java
It's output is:
----------------------------------
balance is 1
balance is 2
balance is 3
balance is 4
balance is 5
balance is 6
balance is 7
balance is 8
balance is 9
balance is 10
balance is 11
balance is 12
balance is 13
balance is 14
balance is 15
balance is 17
balance is 18
balance is 16
balance is 19
balance is 20
balance is 21
balance is 22
balance is 23
balance is 24
balance is 25
balance is 26
balance is 27
balance is 28
balance is 29
balance is 30
balance is 32
balance is 31
balance is 33
balance is 34
balance is 36
balance is 37
balance is 38
balance is 35
balance is 39
balance is 40
balance is 41
balance is 42
balance is 43
balance is 44
balance is 45
balance is 46
balance is 47
balance is 48
balance is 49
balance is 50
balance is 51
balance is 52
balance is 53
balance is 54
balance is 55
balance is 56
balance is 57
balance is 58
balance is 59
balance is 60
balance is 61
balance is 62
balance is 63
balance is 65
balance is 66
balance is 67
balance is 68
balance is 69
balance is 70
balance is 71
balance is 64
balance is 72
balance is 73
balance is 74
balance is 75
balance is 77
balance is 78
balance is 79
balance is 80
balance is 76
balance is 81
balance is 82
balance is 83
balance is 84
balance is 85
balance is 86
balance is 87
balance is 88
balance is 89
balance is 91
balance is 92
balance is 93
balance is 94
balance is 95
balance is 96
balance is 97
balance is 98
balance is 99
balance is 90
balance is 100
----------------------------------
Here also that problem is occuring... So, why does not the balance increase continuously, which I think it should, without giving a lesser value at some times?
....
whoofffffffffff (a sigh..).............................................. Thanks folks.
Note: I have put all the three programs in different folders and then run each one of them seperately.