[b]class monitor vs
TRANSCRIPT
-
7/27/2019 [B]Class Monitor Vs
1/6
6/13/13 [B]Class Monitor Vs. Object Monitor[/B] (Threads forum at JavaRanch)
www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor
posted 2/13/2007 12:18 PM
A friendly
place for
programming
greenhorns!
Big Moose Saloon Search | Java FAQ | Recent TopicsRegister / Login
A special promo: Enter your blog post or vote on a blogger to be featured in
an upcoming Journal
JavaRanch Java Forums Java Threads and Synchronization
Author [B]Class Monitor Vs. Object Monitor[/B]
Sree VaRanch Hand
Joined: Jan 28,2007Posts: 38
The following is my effort to differentiate Thread Monitors and priorities between them. Is this sample
code infering more than that?
This my effort is trying to answer an interview question.
The question:
In a class with two sync methods and one static method with sync code block, explain the monitors,
priorities and dependencies?
v iew p la in copy to c l ip board pri nt ?
Note: Tex t content in the code blocks is automatically word-wrapped
01. import java.lang.*;
02.
03. publicclass MyClass {
04. privateint i = 0;
05. publicsynchronizedvoid increment() {
06. i++;
07. System.out.format("increment() : i = %d\n", i);
08. }
09. publicsynchronizedvoid decrement() {
10. i--;
11. System.out.format("decrement() : i = %d\n", i);
12. }
13.
14. publicstaticvoid methodStaticOne() {
15. synchronized (MyClass.class) {
16. System.out.println("methodStaticOne()");
17. }
18. }
19.
20. publicstaticvoid methodStaticTwo() {
21. synchronized (MyClass.class) {
22. System.out.println("methodStaticTwo()");
23. }
24. }
25.
http://www.javaranch.com/http://www.coderanch.com/t/612895/blogs/contest-vote-blog-posthttp://www.coderanch.com/forums/user/loginhttp://www.coderanch.com/forums/banner/redirect/533http://www.coderanch.com/forums/banner/redirect/533http://www.coderanch.com/forums/banner/redirect/533http://www.coderanch.com/forums/banner/redirect/533http://www.coderanch.com/forums/banner/redirect/533http://www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor#http://www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor#http://www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor#http://www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor#http://www.coderanch.com/forums/user/profile/142628http://www.coderanch.com/forums/f-27/threadshttp://www.coderanch.com/forums/c/1/javahttp://www.coderanch.com/forumshttp://www.javaranch.com/http://www.coderanch.com/t/612895/blogs/contest-vote-blog-posthttp://www.coderanch.com/forums/user/loginhttp://www.coderanch.com/forums/recentTopics/listhttp://www.coderanch.com/how-to/java/Java-FAQhttp://www.coderanch.com/forums/search/filters/27http://www.coderanch.com/forums/banner/redirect/533 -
7/27/2019 [B]Class Monitor Vs
2/6
6/13/13 [B]Class Monitor Vs. Object Monitor[/B] (Threads forum at JavaRanch)
www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor
posted 2/13/2007 5:21 PM
.
27. final MyClass mc = newMyClass();
28.
29. Thread t1 = newThread ( newRunnable () {
30. publicvoid run() {
31. mc.increment();
32. }
33. });
34. Thread t2 = newThread ( newRunnable () {
35. publicvoid run() {
36. mc.decrement();
37. }
38. });
39. Thread t3 = newThread ( newRunnable () {
40. publicvoid run() {
41. MyClass.methodStaticOne();
42. }
43. });
44. Thread t4 = newThread ( newRunnable () {
45. publicvoid run() {
46. MyClass.methodStaticTwo();
47. }
48. });
49.
50. t1.start(); // sync
51. t3.start(); // static
52. t2.start(); // sync
53. t4.start(); // static
54. }
55. }
Result:
v iew p la in copy to c l ip board pri nt ?
Note: T ext c ontent in the code blocks is automatically word-wrapped
01. methodStaticOne()
02. increment() : i = 1
03. decrement() : i = 0
04. methodStaticTwo()
We believe that every being is divine, is God. Every soul is a sun covered over with clouds of ignorance; thedifference between soul and soul is owing to the difference in density of these layers of clouds. - Swami Vivekananda
Chris HurstRanch Hand
Joined: Oct 26,2003Posts: 376
I like ...
The issue with the code is that the threads aren't in a rea l world scenario alive at the same time, by that I
mean they complete so fast they are unlikely to see each other to block against other. You would be better
putting some de lays into the run methods o f each thread and then you might get a better idea of what is
going on.
As a quick example if I were an interviewer I might say ok I ran your code as you sugges ted and got the
results you said but then I did this ...
v iew p la in copy to c l ip board pri nt ?
Note: T ext c ontent in the code blocks is automatically word-wrapped
01. t1.start(); // sync
02. try { Thread.currentThread().sleep(1000); } catch (InterruptedException e) {}
03. t3.start(); // static
04. t2.start(); // sync
05. t4.start(); // static}}
http://www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor#http://www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor#http://www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor#http://www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor#http://www.coderanch.com/how-to/java/BumperStickershttp://www.coderanch.com/forums/user/profile/59745http://www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor#http://www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor#http://www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor#http://www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor# -
7/27/2019 [B]Class Monitor Vs
3/6
6/13/13 [B]Class Monitor Vs. Object Monitor[/B] (Threads forum at JavaRanch)
www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor
posted 2/14/2007 2:23 PM
and got this on my pc (discuss) ...
increment() : i = 1
methodStaticOne()
methodStaticTwo()
decrement() : i = 0
i.e. what I'm trying to check you realise is the threads aren't blocking in the way I think you intended and
you a have a race condition in your code i.e. nothing stops t1 completing (actually regardless of locks but
thats another issue). I couldn't tell if you understood this (which you may have done).
Again if I were you I would play with putting some significant delays into the run methods (without
releasing the locks) and reorder the starts in the main and consider the different results. I think the
example should at least be likely to block and then you can consider class vs object locks.
"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Sree VaRanch Hand
Joined: Jan 28,2007Posts: 38
I introduced delay in each method and changed the calling sequence. In every different calling combination,
the static method with sync code block executed first, then the object sync method, then again static
method w ith sync code block, then the object sync method,....... like round robin.
This happens on W in XP Prof sp2.
v iew p la in copy to c l ip board pri nt ?
Note: T ext c ontent in the code blocks is automatically word-wrapped
01. import java.lang.*;
02.
03. publicclass MyClass {
04. privateint i = 0;
05. publicsynchronizedvoid increment() {
06. i++;
07. try {08. Thread.sleep(1000);
09. }
10. catch (java.lang.InterruptedException iEx) {
11. iEx.printStackTrace();
12. }
13. System.out.format("increment() : i = %d\n", i);
14. }
15. publicsynchronizedvoid decrement() {
16. i--;
17. try {
18. Thread.sleep(1000);
19. }
20. catch (java.lang.InterruptedException iEx) {
21. iEx.printStackTrace();
22. }
23. System.out.format("decrement() : i = %d\n", i);
24. }
25.
26. publicstaticvoid methodStaticOne() {
27. synchronized (MyClass.class) {
28. try {
29. Thread.sleep(1000);
30. }
31. catch (java.lang.InterruptedException iEx) {
32. iEx.printStackTrace();
33. }
34. System.out.println("methodStaticOne()");
35. }
36. }
37.
38. publicstaticvoid methodStaticTwo() {
39. synchronized (MyClass.class) {
http://www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor#http://www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor#http://www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor#http://www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor# -
7/27/2019 [B]Class Monitor Vs
4/6
6/13/13 [B]Class Monitor Vs. Object Monitor[/B] (Threads forum at JavaRanch)
www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor
posted 2/15/2007 3:10 AM
.
41. Thread.sleep(1000);
42. }
43. catch (java.lang.InterruptedException iEx) {
44. iEx.printStackTrace();
45. }
46. System.out.println("methodStaticTwo()");
47. }
48. }
49.
50. publicstaticvoid main(String[] a) {
51. final MyClass mc = newMyClass();
52.
53. Thread t1 = newThread ( newRunnable () {54. publicvoid run() {
55. mc.increment();
56. }
57. });
58. Thread t2 = newThread ( newRunnable () {
59. publicvoid run() {
60. mc.decrement();
61. }
62. });
63. Thread t3 = newThread ( newRunnable () {
64. publicvoid run() {
65. MyClass.methodStaticOne();
66. }
67. });
68. Thread t4 = newThread ( newRunnable () {
69. publicvoid run() {70. MyClass.methodStaticTwo();
71. }
72. });
73.
74. // change any combination of these calls. the 'Result:' is the same on Win XP P
rof sp2.
75. t3.start(); // static
76. t4.start(); // static
77. t1.start(); // sync
78. t2.start(); // sync
79. }
80. }
Result:
v iew p la in copy to c l ip board pri nt ?
Note: T ext c ontent in the code blocks is automatically word-wrapped
01. methodStaticOne()
02. increment() : i = 1
03. methodStaticTwo()
04. decrement() : i = 0
vu leeRanch Hand
Joined: Apr 19,2005Posts: 189 In a class with two sync methods and one static method with sync code block, explain the monitors, priorities and
dependencies?
You may answer the question without looking at the code since how a thread ge tting to a running state is
depended on many factors: OS, number of processors, processor scheduling, and on top of those, the JVM
thread scheduling. Since priority is just a hint to a JVM scheduling, it may completely ignore your priority
setting. That be ing said, if you run this class on different machines , you could get different results. The sync
methods are locked by the object instance while the static methods are locked by the class. These two locks
are totally independent.
thread.start() transits the thread to RUNNABLE state. It's up to the OS scheduler and JVM scheduler to
http://www.coderanch.com/forums/user/profile/97199http://www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor#http://www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor#http://www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor#http://www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor# -
7/27/2019 [B]Class Monitor Vs
5/6
6/13/13 [B]Class Monitor Vs. Object Monitor[/B] (Threads forum at JavaRanch)
www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor
posted 2/16/2007 7:49 PM
.
Chris HurstRanch Hand
Joined: Oct 26,2003Posts: 376
I like ...
Code wise I think you are missing something, all you are doing with your code is playing with race
conditions, the JVM is probably never going to compare thread priorities (across the different lock types) its
just down to which wakes up first ....
First of all I did this (XP2 SP2)
t1.start(); // sync
t2.start(); // sync
t3.start(); // static
t4.start(); // static
methodStaticOne()
increment() : i = 1
methodStaticTwo()
decrement() : i = 0
Notice a non s tatic sync has just beaten a static sync in (not really) ...
Now you might say but a static always gets in first (Not actually predictable behaviour) ... but heres why !
the non class sync does something it does a i ++ so ...
in this case the second thread can (/might) out race the first thread as it has no i++ to do and it goes to
sleep first and therefore wakes up first ...
Proof I remove the i++
v iew p la in copy to c l ip board pri nt ?
Note: T ext c ontent in the code blocks is automatically word-wrapped
01. publicsynchronizedvoid increment()02. {
03. //i++;Removing this means this thread sleeps first (wins the race, may
be :-) )
04. try {
05. Thread.sleep(1000);
06. }
07. catch (java.lang.InterruptedException iEx) {
08. iEx.printStackTrace();}System.out.format("increment() : i = %d\n", i);
09. }
and the none static just won the race (but it was a race) ..........
increment() : i = 0
methodStaticOne()
decrement() : i = 0
methodStaticTwo()
The 'increment' thread won because it went to sleep first and therfore was due to wake up first (not a
priority thing) when it wakes up it can then race the class lock thread into system.out.println vs
system.out.format.
Note I got all sort of results running this code twice (your code as its stands is NOT predictable) i.e. just
because it does the same on your machine everytime doesn't mean it is)
I agree. Here's the link: http://aspose.com/file-tools
http://www.coderanch.com/forums/banner/redirect/535http://www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor#http://www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor#http://www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor#http://www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor#http://www.coderanch.com/how-to/java/BumperStickers -
7/27/2019 [B]Class Monitor Vs
6/6
6/13/13 [B]Class Monitor Vs. Object Monitor[/B] (Threads forum at JavaRanch)
www.coderanch.com/t/233673/threads/java/Class-Monitor-Object-Monitor
subject: [B]Class Monitor Vs. Object Monitor[/B]
Similar Threads
Thread Synchronization Q
ThreadUnsafe
Synchronization of run method
Thread functioning help
synchronized and Thread
All times above are in your local time zone & format.The current ranch time (not your local time) is Jun 13, 2013 03:04:09.
Contact Us | Powered by JForum | Copyright 1998-2013 Paul Wheaton
http://www.javaranch.com/paul-wheaton.jsphttp://www.jforum.net/http://www.coderanch.com/how-to/code/ContactUshttp://www.coderanch.com/t/264288/java-programmer-SCJP/certification/synchronized-Threadhttp://www.coderanch.com/t/267165/java-programmer-SCJP/certification/Thread-functioninghttp://www.coderanch.com/t/550723/java-programmer-SCJP/certification/Synchronization-run-methodhttp://www.coderanch.com/t/238568/java-programmer-SCJP/certification/ThreadUnsafehttp://www.coderanch.com/t/247330/java-programmer-SCJP/certification/Thread-Synchronization