[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Jeanne Boyarsky wrote:Do you know what the synchronized keyword means? That is the key to understanding why the output is always 200. Do you know why the output wouldn't be 200 if that keyword were removed?
Stephan van Hulst wrote:Actually, the program should print two numbers: 200, and any number between 100 and 200 (not necessarily in order). These are all possible outputs:
What have i to do to , to make this code works ?
Norman Radder wrote:
What have i to do to , to make this code works ?
Please explain what "works" means?
To see different results, use a larger number for the counting: for example 1000 vs 100
the two thread to start at the same time,
Norman Radder wrote:
the two thread to start at the same time,
That is very hard to do. Most programs execute one statement at a time. For two statements to be executed at the same time would require that each statement be executed on a separate processor.
That's possible but hard to do.
Norman Radder wrote:Did you try the code with a larger number like 2000 (instead of 100)?
teacher said, find a problem in the code
Norm Radder wrote:
teacher said, find a problem in the code
Don't you need to know what the code is supposed to do to determine if there is a problem?
Norm Radder wrote:What is the program supposed to do? You need to know that to be able to find the problem.
For example if a program is supposed to add two numbers and with the input of 2 and 3 the output was 6 then you know there was a problem because the output for the sum should be 5.
I think that the problem is that the threads starts one after the other,
Norm Radder wrote:
I think that the problem is that the threads starts one after the other,
That is the normal way statements in programs execute: one after the other. It is very hard for two statements to execute at exactly the same time.
Do the two threads execute at the same time? What did your print statements that printed the names show?
Sorry i means threa1 start after the other one has finished
Norm Radder wrote:
Sorry i means threa1 start after the other one has finished
How do you know that? Did you add some print statements to show which thread was executing? Add this to the increment() method to see the order each thread calls it:
maybe the problem is that the thread go to 200 instead of 100
Norm Radder wrote:
maybe the problem is that the thread go to 200 instead of 100
Sorry, without the author of the code writing some comments saying what the program is supposed to do,
There are 100s of things the program does not do. Is one of those 100s of things what the program should do? Impossible to tell.
Campbell Ritchie wrote:Those JavaWorld articles date back to 1998 before the new concurrency classes were introduced. Should one still use the Thread class and Runnables and synchronized?
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |