• Post Reply Bookmark Topic Watch Topic
  • New Topic

A question on synchronized with example  RSS feed

 
Brian Tkatch
Bartender
Posts: 598
26
Linux Notepad Oracle
  • Mark post as helpful
  • send pies
  • 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.)
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Linux Notepad Oracle
  • Mark post as helpful
  • send pies
  • 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?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!