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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

syncronization confusion!!

 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
Enthuware Software Support
Posts: 4818
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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).]
 
Harpal Singh
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
well I ran this code on WIN 2000 platform...
oh god back to square one...
Harpal
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Thanks Ajith,
I do beleive you whole Heartedly........
Harpal
 
Paul Anilprem
Enthuware Software Support
Posts: 4818
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Fine Paul. So we're on the same boat now. Hope there are no more confusions...
Ajith
 
gautam shah
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
Harpal Singh
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
[ 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
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
[ 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
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
[ 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
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
[ 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).]
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
[ 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).]
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
[ 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
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
That is a really big piece of pie for such a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
    Bookmark Topic Watch Topic
  • New Topic