• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread in java

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)

 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Saloon Keeper
Posts: 15490
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:/
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But yes it's hard for me to find the problem :/
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(i mean the thread start after the other one has finished)
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic