File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange output

 
Anudeep Duvvuri
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think is "strange" about the output you provided?
 
Anudeep Duvvuri
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • 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: 1951
7
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • 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.

  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic