aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes syncronization confusion!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply locked New topic
Author

syncronization confusion!!

Harpal Singh
Ranch Hand

Joined: Oct 10, 2000
Posts: 229
Dear friends ,
the following code is doing time slicing but I am putting my run method in synchronized code....should't Thread#0 finish and then Thread#1 start........explain me why is this happening...
<pre>
public class SelfishRunner extends Thread {
private int tick = 1;
private int num;
public SelfishRunner(int num) {
this.num = num;

}
synchronized public void run() {
while (tick < 400000) {
tick++;
if ((tick % 50000) == 0)
System.out.println("Thread #" + num + ", tick = " + tick);
}
}
public static void main(String args[])
{
SelfishRunner sr = new SelfishRunner(0);
SelfishRunner sr1 = new SelfishRunner(1);

sr.start();
sr1.start();
}
}</pre>

result is:
<pre>
---------- run ----------
Thread #0, tick = 50000
Thread #0, tick = 100000
Thread #0, tick = 150000
Thread #1, tick = 50000
Thread #1, tick = 100000
Thread #1, tick = 150000
Thread #1, tick = 200000
Thread #0, tick = 200000
Thread #0, tick = 250000
Thread #0, tick = 300000
Thread #0, tick = 350000
Thread #0, tick = 400000
Thread #1, tick = 250000
Thread #1, tick = 300000
Thread #1, tick = 350000
Thread #1, tick = 400000
Normal Termination
Output completed (1 sec consumed).
</pre>
Harpal
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
I am guessing the VM on your machine has a thread scheduler that uses time-sliced algorithm instead of preemptive multithreading.
The observed behaviour of such a code depends entirely on the thread scheduler which is platform( and vendor ) dependent. If you run the same program on a different OS, you will probably observe a new pattern of output.
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3312
    
    7
This has nothing to do with Thread scheduler etc.
Your run method is synchornized but you are creating two thread objects in main. Both are different objects and so both the threads can get the lock of themselves while entering the run method. They are not tring to get a lock on the same object.
Remember, a thread will wait only if multiple threads try to
accuire a lock on the SAME object.

-Paul.

------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus

[This message has been edited by Paul Anil (edited November 15, 2000).]


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Harpal Singh
Ranch Hand

Joined: Oct 10, 2000
Posts: 229
Thanks Ajith and Paul...
But if Ajith you are right then I donot have to worry about not understanding the concepts of synchro........
Paul if you are right then I am in fix...Paul,can you please modify my programme and explain your point,I am giving the exam next Thursday i.e.23 Nov. I am really worried about Threads......I understand the concept theoritacally but practicals are giving me problem...Please modify my programme...
Harpal
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Harpal,
This is the output I got on my machine when I ran your code without any modifications

Thread #0, tick = 50000
Thread #0, tick = 100000
Thread #0, tick = 150000
Thread #0, tick = 200000
Thread #0, tick = 250000
Thread #0, tick = 300000
Thread #0, tick = 350000
Thread #0, tick = 400000
Thread #1, tick = 50000
Thread #1, tick = 100000
Thread #1, tick = 150000
Thread #1, tick = 200000
Thread #1, tick = 250000
Thread #1, tick = 300000
Thread #1, tick = 350000
Thread #1, tick = 400000

I am using Windows NT (SP5) box with JDK 1.2.2
When you start two threads, you have no way of predicting which will run first, and whether one will allow the other to run intermittently. Calling the start() method on a thread object simply adds it to the ("ready to run") thread pool and after that it is under the mercy of the Thread Scheduler.
The fact that the same program produces two different outputs on two different machines proves that the running platforms have different implementations of the scheduler.
I agree with Paul about each Thread object having its own run method. Infact the <code>synchronized</code> run method should produce the same result as an unsynchronized run method since the method is being executed by only one thread and each thread locks up itself when executing the run method. But your program demonstrates a different concept - the unpredictability of the results when more than one thread is contesting the CPU time to run, which does depend on the thread scheduler.
Ajith
Harpal Singh
Ranch Hand

Joined: Oct 10, 2000
Posts: 229
Ajith,
Thanks a lot......You know what today I booked the time for next thursday and came..there I met two girls who just finished there Exam and One of them Failed 54% and other got 61%...and they werecribbing abt THREADS....I got a bit panicky and on top of that when I thought I knew synchro..well this was the out put...Ajith Thanks aton......feeling so much of relief....
Thanks a lot,
will give you good news of getting in 90's next Thursday,
Harpal
gautam shah
Ranch Hand

Joined: Oct 29, 2000
Posts: 72
Ajith
are you sure that you don't modify the harpal's code in any manner and on running of that code on winNT machine it produces the output shown by you ? I become curious abount winNT unpredictible behaviour, although I am a MCSE and according to me winNT (any version , any survice pack) can't produce output shown by you without modifying harpal's code .
because whatever you are saying is just impossible with winNT always (it may happenes in 1 of 1000)
Harpal Singh
Ranch Hand

Joined: Oct 10, 2000
Posts: 229
well I ran this code on WIN 2000 platform...
oh god back to square one...
Harpal
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Gautam,
I was only talking about unpredictability of running multiple threads across different platforms, not on WinNT only!! Well, I can't prove to you that I didn't modify Harpal's code, so you'll just have to belive me
The output I enclosed was from one run. When I ran the same program several times continuously, I got different outputs. Some output samples are given below -

D:\MYJAVA\MyProjects\JRANCHQS>java SelfishRunner
Thread #0, tick = 50000
Thread #0, tick = 100000
Thread #0, tick = 150000
Thread #0, tick = 200000
Thread #0, tick = 250000
Thread #1, tick = 50000
Thread #0, tick = 300000
Thread #1, tick = 100000
Thread #0, tick = 350000
Thread #1, tick = 150000
Thread #0, tick = 400000
Thread #1, tick = 200000
Thread #1, tick = 250000
Thread #1, tick = 300000
Thread #1, tick = 350000
Thread #1, tick = 400000
D:\MYJAVA\MyProjects\JRANCHQS>java SelfishRunner
Thread #0, tick = 50000
Thread #0, tick = 100000
Thread #0, tick = 150000
Thread #0, tick = 200000
Thread #0, tick = 250000
Thread #0, tick = 300000
Thread #0, tick = 350000
Thread #1, tick = 50000
Thread #0, tick = 400000
Thread #1, tick = 100000
Thread #1, tick = 150000
Thread #1, tick = 200000
Thread #1, tick = 250000
Thread #1, tick = 300000
Thread #1, tick = 350000
Thread #1, tick = 400000

D:\MYJAVA\MyProjects\JRANCHQS>java SelfishRunner
Thread #0, tick = 50000
Thread #0, tick = 100000
Thread #0, tick = 150000
Thread #0, tick = 200000
Thread #1, tick = 50000
Thread #0, tick = 250000
Thread #1, tick = 100000
Thread #0, tick = 300000
Thread #1, tick = 150000
Thread #0, tick = 350000
Thread #1, tick = 200000
Thread #0, tick = 400000
Thread #1, tick = 250000
Thread #1, tick = 300000
Thread #1, tick = 350000
Thread #1, tick = 400000

The point I am trying to drive home is that, when you start two or more threads, you simply cannot tell how they will run.
Ajith
Harpal Singh
Ranch Hand

Joined: Oct 10, 2000
Posts: 229
Thanks Ajith,
I do beleive you whole Heartedly........
Harpal
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3312
    
    7
Ajith, I didn't mean say you were worng. (My Guess) Harpal thought that as he has used sync., one thread should execute after another. It would have been the case had he used sync. on same object. And I meant "this" has nothing to with scheduler as both the threads are accuiring lock on different objects.
-Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Fine Paul. So we're on the same boat now. Hope there are no more confusions...
Ajith
gautam shah
Ranch Hand

Joined: Oct 29, 2000
Posts: 72
i believe you ajith.
as i all ready write that output shown by you comes only
1 of 1000 . because i worked with winNT a lot thats why i react.
by the way harpal, your code doesn't have any relation with synchronization. by only applying the keyword synchronized we can't be sure that this code can't be accessed simultaneously.
this need some more reading about thread concepts Harpal Singh Ji
(just my advise politely).
Harpal Singh
Ranch Hand

Joined: Oct 10, 2000
Posts: 229
Gautam shah ji,
thanks for your advice...will really appreciate if you could modify my code to show me the concept of synchronization...as I feel your concepts seem to be very clear.......
Harpal
Aniruddha Mukhopadhyay
Ranch Hand

Joined: Nov 15, 2000
Posts: 59
Hi Harpal,
If you want synchronization on any object, say string sr do it as below. I have modified your code to run String sr first.
Hope this helps,
Aniruddha
class SelfishRunner extends Thread {
private int tick = 1;
private int num;
static SelfishRunner sr;
public SelfishRunner(int num) {
this.num = num;
}
synchronized public void run() {
synchronized(sr){ while (tick < 400000) {
tick++;
if ((tick % 50000) == 0)
System.out.println("Thread #" + num + ", tick = " + tick);
}}
}
public static void main(String args[]){
sr = new SelfishRunner(0);
SelfishRunner sr1 = new SelfishRunner(1);sr.start();
sr1.start();
}
}

------------------
Aniruddha


Aniruddha
Harpal Singh
Ranch Hand

Joined: Oct 10, 2000
Posts: 229
Thanks ani,
I got it but instead of hardcoding sr in synchronized it will be great to put "this" in it....which ever thread enter the block first should be executed...and the other thread shld wait for the cpmletion of first...can you give it a try..coz when I tried it gave me result as before...
Harpal
Aniruddha Mukhopadhyay
Ranch Hand

Joined: Nov 15, 2000
Posts: 59
Hi Harpal,
Putting this will mean both "objects" will own it (all instances will be "this" for them!).
So programmer has to decide which thread should run first and create lock on that instance.
I do hope I am making sense, because I logged into Javaranch only today!
Aniruddha
gautam shah
Ranch Hand

Joined: Oct 29, 2000
Posts: 72
harpal singh ji
here is your awaited code . have a look!
i made one change to clear it & that is the num variable
given by you & provided in the SelfishRunner creating statement,
i replace it with String name.it prints now what string argument is given at the time of construction of SelfishRunner.
& try yo understand
1)what synchronization is.
2)what is the thing which every one called lock.
3)what is thread & where it exist.
4)which one thread implementation technique is better- Runnable or extends .
etc, etc , etc ...... & the list is going on. on & on.

one More thing harpal singh ji ,
My concepts not only "seen to be very clear" but they are !
because these concepts are developed by myself with the help of great writer's book's and keeping myself many couples of hours inside it.
one more thing these kind of discussions doesn't help for
building solid approach to any concept. but they are good for trouble shooting only.
according to me "NO ONE CAN HELP TO ANY ONE EXCEPT MAN HIMSELF"
<pre>
class SelfishRunner extends Thread {
private int tick = 1;
private String name; //although it's not required
public SelfishRunner(){}
public SelfishRunner(String name,Runnable r) {
super(r,name);
this.name = name;
}
class gautam implements Runnable {
synchronized public void run() {
while (tick < 400000) {
tick++;
if ((tick % 50000) == 0)
System.out.println("Thread #" + Thread.currentThread().getName() +
", tick = " + tick);
}
tick = 1;
} }
public static void main(String args[])
{
SelfishRunner.gautam r = new SelfishRunner().new gautam();
SelfishRunner sr = new SelfishRunner("sr",r);
SelfishRunner sr1 = new SelfishRunner("sr1",r);
sr.start();
sr1.start();
}
}
</pre>


[This message has been edited by gautam shah (edited November 16, 2000).]
gautam shah
Ranch Hand

Joined: Oct 29, 2000
Posts: 72
singh sahab,
here is another example . if still you don't getting it, then write me i will"GIVE YOU MORE". actully i am having full controll on thread . there is nothing which i can't do with thread. and above all, the work shown here i can accomplish in at least 10 more different ways in such a way that you feel giddy.
rest all is same as above...
<pre>
public class SelfishRunner extends Thread {
private int tick = 1;
private String name;//although it's not required
public SelfishRunner(){}
public SelfishRunner(String name,Runnable r) {
super(r,name);
this.name = name;
}
Runnable gautam = new Runnable() {
synchronized public void run() {
while (tick < 400000) {
tick++;
if ((tick % 50000) == 0)
System.out.println("Thread #" + Thread.currentThread().getName()
+ ", tick = " + tick);
}
tick = 1;
} };
public static void main(String args[])
{
SelfishRunner r = new SelfishRunner();
SelfishRunner sr = new SelfishRunner("GAUTAM",r.gautam);
SelfishRunner sr1 = new SelfishRunner("sr1",r.gautam);
SelfishRunner sr2 = new SelfishRunner("sr2",r.gautam);
SelfishRunner sr3 = new SelfishRunner("sr3",r.gautam);
SelfishRunner sr4 = new SelfishRunner("HARPAL",r.gautam);

sr.start();
sr1.start();
sr2.start();
sr3.start();
sr4.start();
}
}
</pre>

" IS DIL MANGE MORE ? "
please forget formatting mistakes because i don't know HTML or any such a kind of well formatting style.

[This message has been edited by gautam shah (edited November 16, 2000).]
Harpal Singh
Ranch Hand

Joined: Oct 10, 2000
Posts: 229
Thanks shah ji,
Thanks for the code ,will go through it
[ Ajith deleted a part of this message that had no significance to the technical discussion ]
Thanks anyways,
Hey thanks Anirudh,
Harpal


[This message has been edited by Ajith Kallambella (edited November 17, 2000).]
gautam shah
Ranch Hand

Joined: Oct 29, 2000
Posts: 72
[ Ajith deleted a part of this message that had no significance to the technical discussion ]
[This message has been edited by Ajith Kallambella (edited November 17, 2000).]
Harpal Singh
Ranch Hand

Joined: Oct 10, 2000
Posts: 229
[ Ajith deleted a part of this message that had no significance to the technical discussion ]
[This message has been edited by Ajith Kallambella (edited November 17, 2000).]
gautam shah
Ranch Hand

Joined: Oct 29, 2000
Posts: 72
[ Ajith deleted a part of this message that had no significance to the technical discussion ]
[This message has been edited by Ajith Kallambella (edited November 17, 2000).]
Harpal Singh
Ranch Hand

Joined: Oct 10, 2000
Posts: 229
[ Ajith deleted a part of this message that had no significance to the technical discussion ]
[This message has been edited by Ajith Kallambella (edited November 17, 2000).]
Adeeshwar Singhal
Greenhorn

Joined: Oct 18, 2000
Posts: 28
[ Ajith deleted a part of this message that had no significance to the technical discussion ]

Cheers everybody.... This is a site for the most respected thing of the world. And we have the responsibility of maintaining it......:-)
Adeesh
[This message has been edited by Ajith Kallambella (edited November 17, 2000).]


Hey guys!! come and have some cups of HOT JAVA........<BR>Adeesh
mohit joshi
Ranch Hand

Joined: Sep 23, 2000
Posts: 243
[ Ajith deleted a part of this message that had no significance to the technical discussion ]
[This message has been edited by Ajith Kallambella (edited November 17, 2000).]
Harpal Singh
Ranch Hand

Joined: Oct 10, 2000
Posts: 229
Mohit and Adeeshwar,
Thanks for understanding me!!!
[ Ajith deleted a part of this message that had no significance to the technical discussion ]
[This message has been edited by Ajith Kallambella (edited November 17, 2000).]
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
People!! People!!
Please use your descretion when posting messages. Avoid digression from the main topic and posting anything that is not related to the technical discussion as it is not going to help anybody.
Remember that your messages will be read by hundreds of other JavaRanch users in months to come. Please show respect to others and avoid adding fuel to the fire. Practice a policy of tolerance when you see something that intimidates you.
I am sorry, but I had to remove unnecessary messages under this thread to avoid further damage. Under normal circumstances I never edit a message posted by another user, but this time it became inevitable. You all have been great contributors to this forum and I encourage you to give in what you can to preserve the quality of discussions here and make it a great place for all.
Thank you for your understanding and cooperation.
Ajith
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: syncronization confusion!!