• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Strange output

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The output of the above program is
Thread-1
static method syncronized 0
Thread-1
static method syncronized 1
Thread-1
static method syncronized 2
Thread-1
static method syncronized 3
Thread-1
static method syncronized 4

Thread-2
static method syncronized 0
Thread-2
static method syncronized 1
Thread-2
static method syncronized 2
Thread-2
static method syncronized 3
Thread-2
static method syncronized 4

Thread-1
static method 2 synchronized 0
Thread-1
static method 2 synchronized 1
Thread-1
static method 2 synchronized 2
Thread-1
static method 2 synchronized 3
Thread-1
static method 2 synchronized 4
Thread-2
static method 2 synchronized 0
Thread-2
static method 2 synchronized 1
Thread-2
static method 2 synchronized 2
Thread-2
static method 2 synchronized 3
Thread-2
static method 2 synchronized 4
 
Anudeep Duvvuri
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is another program which is almost similar to the above one.
But the output is not as the above one.



output:
Hai starts : Thread-1
method1***************0
method1***************1
method1***************2
method1***************3
method1***************4
Hai Ends : Thread-1
Other1 starts : Thread-1
method2-------0
method2-------1
method2-------2
method2-------3
method2-------4
Other1 Ends : Thread-1
Hai starts : Thread-2
method1***************0
method1***************1
method1***************2
method1***************3
method1***************4
Hai Ends : Thread-2
Other1 starts : Thread-2
method2-------0
method2-------1
method2-------2
method2-------3
method2-------4
Other1 Ends : Thread-2


Please explain me why both the outputs are different even we use synchronization
Sorry for consuming so much space
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you think is "strange" about the output you provided?
 
Anudeep Duvvuri
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the 1st program thread 2 is starting to print method 1 without the thread 1 printing method 2
But in the 2nd Program Thread2 waits until the 1st Thread prints both the methods 1 and 2
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In both example programs that is purely coincidental; the order is not predictable.
It's also possible for thread 2 to complete invocations of method 1 and 2 before thread 1 gets its turn.
Adding the keyword synchronized to a method only ensures that no two threads can execute that method (or any other body of code that synchonizes on the same lock) concurrently.

 
reply
    Bookmark Topic Watch Topic
  • New Topic