Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread in java  RSS feed

 
Pierre Gadar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, i have to analyse this code and test it many times and explain what happends, but i dont understand this code, can someone explains me please? Thanks;)
(in the console the output is "Total : 200 Total : 200)

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 36622
475
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Pierre Gadar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?


Hi, no, why the output wouldn't be 200 if that keyword were removed?
 
Stephan van Hulst
Saloon Keeper
Posts: 7181
117
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, the program should print two numbers: 200, and any number between 100 and 200 (not necessarily in order). These are all possible outputs:
 
Pierre Gadar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:


I don't have these outputs in console, What have i to do to , to make this code works ? Apparently it's not correct, so must I separate the 2 threadsin 2 class?
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the larger number the two totals are often not the same.
 
Pierre Gadar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


I don't know , there is something to modifie but we don't know what, i think maybe he wants the two thread to start at the same time, and not one after the other, like in this code
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  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
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try the code with a larger number like 2000 (instead of 100)?
 
Pierre Gadar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


I made this, what do you think of this?



 
Pierre Gadar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norman Radder wrote:Did you try the code with a larger number like 2000 (instead of 100)?


if i tested it, but the teacher said, find a prooblem in the code and propose a simple solution:/
 
Norm Radder
Rancher
Posts: 1772
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Pierre Gadar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?


He asked us to analyse and understand the code, then find a problem and proposed a solution :/
 
Pierre Gadar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But yes it's hard for me to find the problem :/
 
Norm Radder
Rancher
Posts: 1772
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pierre Gadar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


yes but he doesn't tell us anything more :/

I think that the problemis that the threads starts one after the other, and i made a solutionin the second code i posted, and the two threads start at the same time and go to 100, but im not sure if it is that, what do you think of it?
 
Pierre Gadar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(i mean the thread start after the other one has finished)
 
Norm Radder
Rancher
Posts: 1772
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Pierre Gadar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1772
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Pierre Gadar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:


yes you are right, the thread dont start after the other one has finish (my mistake) its only if i remove the sleep

then maybe the problem is that the thread go to 200 instead of 100? (looks like they are addittionning themself, if we put an other thread they will all go to 300 :/)
 
Norm Radder
Rancher
Posts: 1772
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pierre Gadar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


yes i understand, i have the same problem, it's not clear
 
Norm Radder
Rancher
Posts: 1772
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps the problem with the code is the lack of comments describing what it is doing.
 
Stephan van Hulst
Saloon Keeper
Posts: 7181
117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I *suppose* that the intention is that the program always prints:
Like Norm said, it's not clear because you didn't give us the literal assignment your professor gave you, and there are no comments in the code to describe what is supposed to happen.

However, from the code it seems likely that the application is supposed to count to hundred twice, and then print the total of one shared counter, both times.

If that is the case, then there are a few ways in which the program COULD fail. I'm capitalizing the word "could", because even though it's a possibility, the chances are unlikely. I've already shown possible outputs of the program which you never get because... well... that's just the way it is.

Jeanne asked you whether you knew what the 'synchronized' keyword means. If you don't, then I don't think you could answer your professor's question satisfactorily, and you should read up on it. The article that helped me the most at that point was this one: http://www.javaworld.com/article/2076797/java-concurrency/programming-java-threads-in-the-real-world--part-2.html

If you DO know what the synchronized keyword means, it should be clear that increment() performs an atomic operation, but since increment() is being called as an isolated statement in a greater method that is not thread-safe, it doesn't really matter in the grand scheme of things.

Let us know what you don't understand, and we can try to clarify.
 
Campbell Ritchie
Sheriff
Posts: 54030
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those JavaWorld articles date back to 1998 before the new concurrency classes were introduced. Shou‍ld one still use the Thre‍ad class and Runnables and synchronized?
 
Norm Radder
Rancher
Posts: 1772
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To see more about what is happening, try this version of your code that has more debug print outs:
 
Stephan van Hulst
Saloon Keeper
Posts: 7181
117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Those JavaWorld articles date back to 1998 before the new concurrency classes were introduced. Shou‍ld one still use the Thre‍ad class and Runnables and synchronized?

Thread and synchronized, no. However, those articles explain very intuitively what object monitors are and how to work with them, and you still need to know about that when working with the new concurrency classes effectively.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!