• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

A question on synchronized with example

 
Bartender
Posts: 598
26
Oracle Notepad Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A first example in Schildt's Java: A Beginner's Guide displays what synchronized does (and a little of what it doesn't, too.) He then says to remove the word synchronized to see what happens. I typed in the example (with minor changes):



With synchronized:


Without synchronized:


Each thread adds to 15 separately, but without synchronized they only add to 29. Why?

Also, how can the second thread start first? I've seen that a few times already, both in this example and the previous one (not shown in this post.)
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason the total is 29 for the unsynchronized method in the example you have shown is as follows:

There is only 1 instance of the SumArray class and so there is only one 'sum' variable which is incremented by both threads when calling the sumArray method.
Child#1 calls sumArray and executes the first line of the method which sets sum to zero. It then executes the first iteration of the for loop which adds the first element of the num array (1) to sum and prints out its value (1). Finally it sleeps.
Child#2 calls sumArray and executes the first line of the method which sets sum to zero. It then executes the first iteration of the for loop which adds the first element of the num array (1) to sum and prints out its value (1). Finally it sleeps.
The for loop continues adding all the remaining elements of the num array (2, 3, 4, 5 = 14) until both threads complete. ie you add 14 and 14 to 1 which = 29.

To answer your other question when you call start on a thread there is no guarantee of when it's run method will actually get called so the order of execution of multiple threads may and probably will change. For example if you start 10 threads you will see the order of execution can change with each run.

 
Brian Tkatch
Bartender
Posts: 598
26
Oracle Notepad Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now i'm embarrased; i missed something obvious. It's being set and then reset. Doh! I guess it's possible (is it?) that the second thread will also set it to zero earlier and hit 30, or less and hit less.

I understand that threads are given different time slices. But shouldn't the first one start first?
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brian Tkatch wrote:
I understand that threads are given different time slices. But shouldn't the first one start first?



The specification doesn't require how threads are started, nor how are they time sliced. This is to allow the underlying OS to schedule threads as needed.

Henry
 
Poop goes in a willow feeder. Wipe with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic