Meaningless Drivel is fun!*
The moose likes Threads and Synchronization and the fly likes Strange output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Strange output" Watch "Strange output" New topic
Author

Strange output

Anudeep Duvvuri
Greenhorn

Joined: May 18, 2012
Posts: 29


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

Joined: May 18, 2012
Posts: 29
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

Joined: Mar 10, 2008
Posts: 1821
    
    7

What do you think is "strange" about the output you provided?

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Anudeep Duvvuri
Greenhorn

Joined: May 18, 2012
Posts: 29
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

Joined: Mar 10, 2008
Posts: 1821
    
    7

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.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Strange output